简体   繁体   English

以编程方式设置为根视图控制器时,视图控制器无法正确显示子视图

[英]View controller not displaying subviews correctly when programmatically set as root view controller

I've been building my view controllers programmatically by setting a custom view in the loadView() method. 我一直通过在loadView()方法中设置自定义视图来以编程方式构建视图控制器。 While changing my root view controller in AppDelegate to a specified view controller, I've noticed that the view controller does not display it's subviews properly laid out, if it even gets displayed. AppDelegate中将我的根视图控制器更改为指定的视图控制器时,我注意到该视图控制器甚至无法显示其子视图的布局正确。 Using the view debugger, I see that the views are seen as being in the view hierarchy but they are no where to be found, so I end up with blank view controller. 使用视图调试器,我看到视图被视为在视图层次结构中,但是在哪里找不到它们,因此我最终得到了空白的视图控制器。 What's odd is that when I instead present the view controller from another view controller, only then is it properly laid out and all views are visible. 奇怪的是,当我改为从另一个视图控制器中呈现视图控制器时,只有这样才能正确布局并显示所有视图。 My view controllers all follow the same creation shown below. 我的视图控制器全部遵循如下所示的相同创建。

class ViewController: UIViewController {


    private var rootView: View? {
        get { return self.viewIfLoaded as? View ?? .none }
        set (view) { self.view = view }
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    init() {
        super.init(nibName: nil, bundle: nil)
    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }



    override func loadView() {
        self.rootView = View()
    }

}

And here is how I am creating my custom views for my view controllers 这就是我为视图控制器创建自定义视图的方式

class View: UIView {

    // Lazy declare views here

    fileprivate var shouldSetupConstraints = true


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

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


    override func updateConstraints() {
        if(shouldSetupConstraints) {

            // Adding constraints here

            shouldSetupConstraints = false
        }
        super.updateConstraints()
    }


    func setupView() {

        // Adding subviews here 

    }

}

Here is how I am setting the root view controller in my AppDelegate 这是我在AppDelegate设置根视图控制器的方式

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

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.backgroundColor = Palette.backgroundColor
    window?.rootViewController = MyViewController()
    window?.makeKeyAndVisible()
    return true

}

I expect my programmatically laid out view controllers to be displayed properly while being set as the root view controller, but instead the views are not visible or not laid out properly. 我希望以编程方式布置的视图控制器在设置为根视图控制器时能够正确显示,但是视图不可见或布置不正确。 They are only displaying correctly when the view controller is presented by another view controller. 仅当另一个视图控制器显示视图控制器时,它们才能正确显示。

Looks like this behavior was being caused by not setting 似乎此行为是由于未设置引起的
self.translatesAutoresizingMaskIntoConstraints = false
in setupView() in my custom view. 在我的自定义视图中的setupView()中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM