ios 从xib创建UITabBarController时黑屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49034546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Black screen when creating UITabBarController from xib
提问by Eugene Karambirov
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.
我尝试UITabBarController
从xib创建。所以我在 xib 中设置选项卡项目,为这样的控制器连接类和 xibs 名称。以全分辨率打开图像。
Then I set TabBarController as root view controller.
然后我将 TabBarController 设置为根视图控制器。
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?
我可以以UITabBarController
编程方式创建,所以问题是:如何获得我在 xib 中创建的内容?
回答by Jacob King
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.
必须以一种特殊的方式加载 nib 中的视图控制器,否则加载类时无需任何支持 UI。
Create the following method in TabBarController
:
在 中创建以下方法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:
现在,在您的 中AppDelegate
,像这样调用它:
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = TabBarController.instantiateFromNib()
window?.makeKeyAndVisible()
Give this a try.
试试这个。