[英]Use Storyboard References While Retaining Icons & Text for Tab Bar Controller
I started refactoring one of my projects to make the code easier to manage and the Tab Bar Controller lost its icons for which tab represents what.我开始重构我的一个项目以使代码更易于管理,并且 Tab Bar Controller 丢失了用于哪个选项卡代表什么的图标。 Without this I'm a bit lost which tab is what for re-ordering purposes.
没有这个,我有点迷失哪个选项卡用于重新排序。
How do I get the icons to show up again for a tab bar controller when I'm using storyboard references?当我使用故事板引用时,如何让标签栏控制器的图标再次显示?
Refer to my attached image.参考我所附的图片。 Notice how the first 3 tabs are 'blank' but the other tabs that I have not refactored yet show with the icons and titles.
请注意前 3 个选项卡是“空白”的,但我尚未重构的其他选项卡如何显示图标和标题。
For those unfamiliar with storyboard references I was following the tutorial here: http://code.tutsplus.com/tutorials/ios-9-staying-organized-with-storyboard-references--cms-24226对于那些不熟悉故事板参考的人,我正在关注这里的教程: http : //code.tutsplus.com/tutorials/ios-9-staying-organized-with-storyboard-references--cms-24226
Sample of Tasks Storyboard to show Icon set Correctly任务故事板示例以正确显示图标集
First, in the storyboard where the tab bar controller is , there should be a scene for the referenced storyboard.首先,在标签栏控制器所在的storyboard中,应该有被引用storyboard的场景。
Just click on the scene that tab is associated with and click the tab bar at the bottom, then go to the attributes inspector, and you'll be able to assign a new icon to it.只需单击与选项卡关联的场景,然后单击底部的选项卡栏,然后转到属性检查器,您就可以为其分配一个新图标。
Update - This approach no longer appears to work in Xcode 9.更新- 这种方法似乎不再适用于 Xcode 9。
Here's how to get the tab to show properly:以下是让选项卡正确显示的方法:
Click the Editor menu and choose Refactor to Storyboard...单击 Editor 菜单并选择 Refactor to Storyboard...
The UITabBarController tab will now point to the new storyboard reference... UITabBarController 选项卡现在将指向新的故事板参考...
For some weird reason, I wasn't able to see the tab bar in my reference view controller in IB.出于某种奇怪的原因,我无法在 IB 的参考视图控制器中看到选项卡栏。 Although while selecting it and expanding the Document Outline, I was able to see it in my view list.
虽然在选择它并展开文档大纲时,我能够在我的视图列表中看到它。 I could make my changes through it.
我可以通过它做出改变。
Hope this helps!希望这有帮助! :)
:)
XCode 11.1: The following approach gets the desired tab title and icon to show at runtime: XCode 11.1:以下方法获取所需的选项卡标题和图标以在运行时显示:
Title
and Image
properties in the Bar Item section of the properties panel.Title
和Image
属性。 At this point, the correct title should appear at runtime (but not at compile time in the storyboard editor).此时,正确的标题应该出现在运行时(但不是在故事板编辑器中的编译时)。 If the icon is there too, great.
如果图标也在那里,那就太好了。 If it's not, you can try checking that the image reference is valid and located in the same module as the tab bar item (ie in the same framework).
如果不是,您可以尝试检查图像引用是否有效并与选项卡栏项位于同一模块中(即在同一框架中)。 If it still doesn't appear, here's a hackish workaround that will work:
如果它仍然没有出现,这里有一个可行的黑客解决方法:
UITabBarController
.UITabBarController
的新类。 Override viewWillLayoutSubviews
as follows:覆盖
viewWillLayoutSubviews
如下:
override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() // Set the tab bar image at runtime if let exampleTab = tabBar.items?.first(where: { $0.title == "ExampleTitle" }) { // To insert an image literal, type "Image" and select Image Literal // from the autocomplete menu. An icon should appear. // Double-click the icon and select the desired image from the grid. exampleTab.image = Image Literal } }
Finally, the tab bar item should now appear correctly at runtime.最后,标签栏项目现在应该在运行时正确显示。 If anyone knows how to make it appear correctly at compile time too (in the storyboard editor) for a storyboard in an external framework, let me know.
如果有人知道如何使其在编译时(在故事板编辑器中)在外部框架中的故事板也正确显示,请告诉我。
If you embed a navigation controller before your scene, you then can edit it like normal.如果您在场景之前嵌入导航控制器,则可以像往常一样对其进行编辑。
Select your storyboard reference then:选择您的故事板参考,然后:
Go to the top and click Editor > Embed > Navigation Controller转到顶部并单击编辑器 > 嵌入 > 导航控制器
Tab bar controller with embedded navigation controller and linking storyboard reference带有嵌入式导航控制器和链接故事板参考的标签栏控制器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.