Child 视图永远不可见

Child view is never visible

我正在以编程方式构建 UI,但在为 children 视图呈现元素时遇到了一些麻烦。

我正在通过实例化我的初始视图控制器和它需要的视图来设置应用程序 Window

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let initialView = StartupView(frame: CGRect.zero)
        let startupViewController = StartupViewController(startupView: initialView);

        window = UIWindow(frame: UIScreen.main.bounds);
        window!.rootViewController = startupViewController;
        window?.backgroundColor = UIColor.white
        window!.makeKeyAndVisible();

        return true
    }
}

StartupView class 实例化一个添加到自身的 UIButton 并设置约束以指定 height/width 并水平和垂直居中对齐。

class StartupView : UIView {

    // views
    var newAccountButton: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.configureView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.configureView()
    }

    override func updateConstraints() {
        self.newAccountButton.translatesAutoresizingMaskIntoConstraints = false
        self.newAccountButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        self.newAccountButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.newAccountButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
        self.newAccountButton.widthAnchor.constraint(equalToConstant: 50).isActive = true

        print("\(self.newAccountButton.titleLabel?.text ?? "Unknown") is \(self.newAccountButton.frame)")
        super.updateConstraints()
    }

    func configureView() {
        // New account button setup
        newAccountButton = UIButton(frame: CGRect.zero)
        newAccountButton.titleLabel?.text = "Create Account"

        self.addSubview(newAccountButton)
    }
}

StartupView赋予其ViewController。似乎分配给 StartupViewViewController 约束正在工作,因为我已经验证我的 StartupView 本身的大小确实等于 ViewController 视图的大小。但是,当我 运行 模拟器时,我没有看到 UIButton.

class StartupViewController: UIViewController {
    var startupView: StartupView!

    init(startupView: StartupView) {
        self.startupView = startupView
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.startupView = StartupView(frame: CGRect.zero)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.startupView)

        self.startupView.translatesAutoresizingMaskIntoConstraints = false

        self.startupView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.startupView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.startupView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        self.startupView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

        self.startupView.updateConstraints()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我是不是做错了约束?或者我添加嵌套视图的顺序重要吗?

编辑

这是视图调试器的屏幕截图。

我可以在布局中看到按钮(突出显示),但看不到正在呈现的实际按钮。写入 print("\(self.newAccountButton.titleLabel?.text): \(self.newAccountButton.frame)") 会写出按钮的标题及其框架,但不会呈现任何内容。

我认为您可以简单地这样做,而不是覆盖 updateConstraints:

编辑

与 OP 聊天讨论后的最终解决方案(不直接访问标签):

    func configureView() {
        // New account button setup
        newAccountButton = UIButton(frame: CGRect.zero)
        newAccountButton.setTitleColor(self.tintColor, for: UIControlState.normal)

        newAccountButton.setTitle("Create Account", for: UIControlState.normal)

        self.addSubview(newAccountButton)
        self.newAccountButton.translatesAutoresizingMaskIntoConstraints = false
        self.newAccountButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        self.newAccountButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.newAccountButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
        self.newAccountButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
    }

我认为在您的应用委托中,您必须定义 StartupView 的大小。目前设置为 zero

let initialView = StartupView(frame: CGRect.zero).