子视图永远不可见

时间:2017-07-31 06:46:46

标签: ios swift swift3 nslayoutconstraint

我以编程方式构建我们的UI,并且在为子视图渲染元素时遇到了一些麻烦。

我通过实例化我的初始视图控制器及其所需的视图来设置应用程序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类实例化它添加到自身的单个UIButton,并设置约束以指定高度/宽度,并将中心水平和垂直对齐。

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。似乎分配给ViewController的{​​{1}}约束正在起作用,因为我已经验证我的StartupView确实将自身的大小设置为等于StartupView视图的大小。当我运行模拟器时,我没有看到ViewController

UIButton

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

修改

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

enter image description here

我可以看到布局中的按钮(突出显示),但我看不到正在渲染的实际按钮。写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. } } 写出按钮的标题及其框架,但没有任何内容呈现。

2 个答案:

答案 0 :(得分:1)

我认为不是覆盖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
    }

答案 1 :(得分:0)

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

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