子视图不在xcode playground中工作

时间:2018-03-30 14:06:49

标签: ios swift xcode subclass swift-playground

我试图在我的主视图中添加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()

3 个答案:

答案 0 :(得分:2)

你做错了很多:(

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

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

结果:

enter image description here

答案 1 :(得分:1)

loadViewUIViewController生命周期回调方法(请参阅docs)。

您的SecondViewUIView的子类,未定义loadView。因此,您在loadView中实现的SecondView不会被环境调用。您必须明确地调用它:

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

sView.loadView()

view.addSubview(sView)

此外,我建议将其重命名(以便与UIViewController loadView混淆。

<强>更新

还有其他一些错误。

1 :在第二个视图的loadView中,您要创建新的SecondView并为其添加标签。您需要将其添加到self

2 :在viewController的loadView中,您正在使用view创建一个新的UIView(),默认为UIView(frame: CGRect.zero),然后您设置sView.frame = view.frame也将构成第二个视图CGRect.zero的框架。 view环境会延长UIViewController,但sView的框架不会更新。有几个选项如何做,但最简单的方法是从一开始就用view初始化frame。工作场所解决方案:

import UIKit
import Foundation
import PlaygroundSupport

class SecondView : UIView{

    func loadView() {
        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

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

}

class MyViewController : UIViewController {

    //Outlets
    let profileButton = UIButton()

    override func loadView() {
        let view = UIView(frame: UIScreen.main.bounds)

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

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

        view.addSubview(sView)

        print(sView.frame)

        self.view = view

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

PlaygroundPage.current.liveView = MyViewController()

答案 2 :(得分:1)

宣布子视图的正确方法

class SecondView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        sharedLayout()

    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder : aDecoder)
        sharedLayout()
    }

    func sharedLayout()
    {
         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
         self.addSubview(label)
         print("Test")
    }

}