我试图将UIView类型的子视图添加到主视图中。但是,这无法正常工作。我认为这应该将屏幕更改为红色并显示文本。你们中的任何一个都知道问题可能是什么吗?

import UIKit
import Foundation
import PlaygroundSupport

class SecondView : UIView{

  func loadView() {
     let secondViewController = SecondView()
     let label = UILabel()
     label.frame = CGRect(x: 0, y: 335, width: 400, height: 53)

     label.textAlignment = .center
     label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
     label.text = "Hejsan"
     label.textColor = .white

     secondViewController.addSubview(label)
      print("Test")
}

 }

  class MyViewController : UIViewController {

   //Outlets
   let profileButton = UIButton()

     override func loadView() {
      let view = UIView()

      view.backgroundColor = .green
      print(view.frame)

     let sView = SecondView()
     sView.backgroundColor = .red
     sView.frame = view.frame

     view.addSubview(sView)

     print(sView.frame)

     self.view = view

   }
 }
// Present the view controller in the Live View window

PlaygroundPage.current.liveView = MyViewController()

最佳答案

您做错了很多事:(

这将在游乐场页面中运行,并且应该与您要的内容接近。检查评论以了解发生了什么:

import UIKit
import Foundation
import PlaygroundSupport

class SecondView : UIView{

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    func commonInit() {

        // create a label
        let label = UILabel()
        // we're going to use auto-layout
        label.translatesAutoresizingMaskIntoConstraints = false

        // add the label to self
        self.addSubview(label)

        // pin the label to leading, trailing and bottom -- all to Zero
        label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0.0).isActive = true
        label.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0.0).isActive = true
        label.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0.0).isActive = true

        label.textAlignment = .center
        label.font = UIFont(name: "helveticaneue-ultraLight", size: 40)
        label.text = "Hejsan"
        label.textColor = .white

    }

}

class MyViewController : UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // we don't need to create a new view
        //let view = UIView()

        view.backgroundColor = .green

        // create the "SecondView"
        let sView = SecondView()
        // we're going to use auto-layout
        sView.translatesAutoresizingMaskIntoConstraints = false

        // add the new view
        view.addSubview(sView)

        // pin the new view to top, leading and trailing -- all to Zero so it fills this view
        sView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0).isActive = true
        sView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0).isActive = true
        sView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
        sView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true

        sView.backgroundColor = .red

    }

}

// Present the view controller in the Live View window

let vc = MyViewController()
vc.preferredContentSize = CGSize(width: 375, height: 667)
PlaygroundPage.current.liveView = vc

结果:

ios -  subview 在XCode Playground中不起作用-LMLPHP

关于ios - subview 在XCode Playground中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49575931/

10-10 14:35