代码之家  ›  专栏  ›  技术社区  ›  David Sundström

子视图在xcode游乐场中不工作

  •  1
  • David Sundström  · 技术社区  · 7 年前

    我试图将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 回复  |  直到 7 年前
        1
  •  2
  •   DonMag    7 年前

    你做了很多错事:(

    这将在操场页面上运行,并且应该接近您想要的内容。查看评论以了解情况:

    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

        2
  •  1
  •   Shehata Gamal    7 年前

    声明子视图的正确方法

    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")
        }
    
    }
    
        3
  •  1
  •   Milan Nosáľ    7 年前

    loadView 是一个 UIViewController 生命周期回调方法(请参阅 docs )。

    你的 SecondView 是的子类 UIView 这没有定义 加载视图 。因此 加载视图 您在中实现 第二视图 不会被环境调用。您必须明确地调用它:

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

    此外,我建议将其重命名(以便与 UIViewController 加载视图

    更新

    还有其他几个错误。

    1. :在您的 加载视图 您正在创建的第二个视图的 第二视图 并将标签添加到其中。您需要将其添加到 self

    2. :英寸 加载视图 在viewController的 view 具有 UIView() 默认为 UIView(frame: CGRect.zero) 然后你设置 sView.frame = view.frame 这将构成第二个视图的框架 CGRect.zero 看法 将由 UIViewController 环境,但 sView 不会更新。有几种方法可供选择,但最简单的方法是初始化 看法 使用适当的 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()
    
    推荐文章