xcode 标签栏不显示图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45648092/
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
Tab bar not showing icons?
提问by Dani
I read a bunch of related questions, I tried what they said, nothing works really. Not sure why. So, I have 3 different UIStoryboards
. First one is the Auth Storyboard that handles Login/Register and there's a storyboard reference to the second Storyboard - Tab bar storyboard. This storyboard contains 5 other storyboard references that would load it's separate view controllers.
我阅读了一堆相关的问题,我尝试了他们所说的,但没有任何效果。不知道为什么。所以,我有 3 个不同的UIStoryboards
. 第一个是处理登录/注册的 Auth Storyboard,还有一个 Storyboard 引用到第二个 Storyboard - Tab bar storyboard。这个故事板包含 5 个其他故事板引用,它们将加载它的单独视图控制器。
My problem is that the icons are not showing once the user is logged in. I setup a custom color of the first view controller in the UITabViewController
just to make sure it loads. It does.
我的问题是,一旦用户登录,图标就不会显示。我设置了第一个视图控制器的自定义颜色,UITabViewController
以确保它加载。确实如此。
I've tried to setting all images/icons to render them as "Original", didn't work. I set a system image, just to see if that's the issue, they are not shown. Also worth mentioning is that the icons are shown in the storyboard but when its being compiled, they're nowhere to be seen in the simulator.
我试图设置所有图像/图标以将它们呈现为“原始”,但没有用。我设置了一个系统映像,只是为了看看这是否是问题,它们没有显示。另外值得一提的是,图标显示在情节提要中,但是当它被编译时,它们在模拟器中无处可见。
What am I doing wrong?
我究竟做错了什么?
PS: I've changed the tint color of the tab bar, just to test it, and it works. So, the problem is not with that...
PS:我改变了标签栏的色调,只是为了测试它,它可以工作。所以,问题不在于...
回答by Mark Moeykens
Problem With Storyboard References and Missing Tab Bar Icons
故事板参考和缺少标签栏图标的问题
If you have Storyboard References attached to your Tab Bar Controller you will notice that the storyboard reference actually has a UITabBarItemon it:
如果您将 Storyboard References 附加到您的 Tab Bar Controller,您会注意到 Storyboard References 实际上有一个UITabBarItem:
Do not set the properties for this "fake" UITabBarItem!
不要为这个“假”UITabBarItem 设置属性!
Your settings will do nothing at all when you run the app. The values you provide ONLY affect what you see in Interface Builder, not your app when you run it.
当您运行该应用程序时,您的设置将什么也不做。您提供的值仅影响您在 Interface Builder 中看到的内容,而不会影响您运行时的应用程序。
Solution
解决方案
What you need to do is go to the ACTUAL view controller that your storyboard reference is pointing to. That view controller will also have a UITabBarItem on it. This is the "real" UITabBarItem. Set your properties (icon, title, etc.) there and you will see them show when you run the app.
您需要做的是转到您的故事板参考所指向的 ACTUAL 视图控制器。该视图控制器上还将有一个 UITabBarItem。这是“真正的”UITabBarItem。在那里设置您的属性(图标、标题等),您将在运行应用程序时看到它们显示。
回答by Rahul Kumar
I have attached image describing connections:
我附上了描述连接的图像:
Connect TabBar Controller to your five View Controllers
For each View Controller, select any View Controller -> Go to Editor in Xcode options at top -> Embed In -> Navigation Controller
- Select bottom bar in Navigation Controller -> open Attribute Inspector in Utilities -> provide the your title and image as highlighted in image
将 TabBar 控制器连接到你的五个视图控制器
对于每个视图控制器,选择任何视图控制器 -> 在顶部的 Xcode 选项中转到编辑器 -> 嵌入 -> 导航控制器
- 在导航控制器中选择底部栏 -> 在实用程序中打开属性检查器 -> 提供您在图像中突出显示的标题和图像
Navigation from Login/Register to TabBar Screen:Provide the Storyboard ID to TabBarController eg. "TabBarController" and create the TabBarController instance using Storyboard ID of corresponding Storyboard.
从登录/注册导航到 TabBar 屏幕:将 Storyboard ID 提供给 TabBarController 例如。“TabBarController”并使用相应 Storyboard 的 Storyboard ID 创建 TabBarController 实例。
Swift 3
斯威夫特 3
func navigateToTabBar() {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "TabBarController") as UIViewController
self.present(nextViewController, animated:true, completion:nil)
}
回答by Joe Andolina
If you are making your navigationController programmatically, you will see the same symptoms due to viewDidLoad being called too late. Overriding init in your child view controllers will get your icons showing properly.
如果您以编程方式制作导航控制器,您将看到相同的症状,因为 viewDidLoad 被调用得太晚了。在您的子视图控制器中覆盖 init 将使您的图标正确显示。
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.tabBarItem = UITabBarItem(title: "Settings", image: UIImage(named: "gear"), tag: 1)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
Happy coding!
快乐编码!
回答by Roozbeh Zabihollahi
I had the same problem. My mistake was I did set the Selected Image
in the "Tab Bar Item", but I did not set the Image
property.
我有同样的问题。我的错误是我确实Selected Image
在“Tab Bar Item”中设置了 ,但我没有设置该Image
属性。
So, the tab bar icon was showing up in the child views [in storyboard] but was not showing up in the parent view (i.e. TabBarController).
因此,标签栏图标显示在子视图 [故事板] 中,但没有显示在父视图中(即 TabBarController)。