简体   繁体   中英

Black screen when creating UITabBarController from xib

I try to create UITabBarController from xib. So I setup tab items in xib, connect classes and xibs names for controllers like that. Open image in full resolution .

像那样

Then I set TabBarController as root view controller.

根视图控制器

As result, I get a black screen with no tab items.

黑屏

I can create UITabBarController programmatically, so the question is: how can I get what I create in xib?

There is a special way in which view controllers within nibs must be loaded, else the class is loaded without any of the backing UI.

Create the following method in TabBarController :

class func instantiateFromNib() -> TabBarController {
    let nib = UINib(nibName: "TabBarController", bundle: nil)
    let vc = nib.instantiate(withOwner: nil, options: nil).first as! TabBarController
    return vc
}

Now, in your AppDelegate , invoke it like so:

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = TabBarController.instantiateFromNib()
window?.makeKeyAndVisible()

Give this a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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