简体   繁体   English

在为标签栏控制器保留图标和文本的同时使用故事板参考

[英]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任务故事板示例以正确显示图标集

在此处输入图片说明

It seems another solution that worked for me without altering the Approach you went through:似乎另一种解决方案对我有用,而不会改变您所经历的方法:

  • Leave Storyboard references as it's保留 Storyboard 引用原样在此处输入图片说明
    • Go to Initial view controller in the referenced storyboard转到引用故事板中的初始视图控制器
    • Add Tab bar item to the scene标签栏项添加到场景中在此处输入图片说明
    • Configure it as you have done in UITabBarController storyboard像在UITabBarController故事板中一样配置它在此处输入图片说明
    • Clean & Run清洁和运行在此处输入图片说明
    • Repeat it for all Storyboard references对所有Storyboard 引用重复此操作

Happy Coding!快乐编码!

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:以下是让选项卡正确显示的方法:

  1. Put the first UIViewController that will be embedded in the tab in the same storyboard as the UITabViewController.将第一个嵌入在标签中的 UIViewController 放在与 UITabViewController 相同的故事板中。 在此处输入图片说明
  2. Ctrl + Drag from the tab bar controller to the content view controller to make the connection as per usual. Ctrl + 从选项卡栏控制器拖动到内容视图控制器以照常进行连接。 This will automatically add the UITabBarItem to the bottom of the content view controller.这将自动将 UITabBarItem 添加到内容视图控制器的底部。 在此处输入图片说明
  3. Select the content view controller.选择内容视图控制器。
  4. Click the Editor menu and choose Refactor to Storyboard...单击 Editor 菜单并选择 Refactor to Storyboard... 在此处输入图片说明

  5. The UITabBarController tab will now point to the new storyboard reference... UITabBarController 选项卡现在将指向新的故事板参考... 在此处输入图片说明

  6. ... and the content view controller preserves the UITabBarItem from the tab bar relationship. ... 并且内容视图控制器保留了标签栏关系中的 UITabBarItem。 It will appear normally in the app now.它现在将正常出现在应用程序中。 在此处输入图片说明

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:以下方法获取所需的选项卡标题和图标以在运行时显示:

  1. Create a storyboard reference to the desired storyboard (including the correct bundle identifier if it is located in an external framework).创建对所需故事板的故事板引用(如果它位于外部框架中,则包括正确的包标识符)。
  2. Ctrl-drag from the tab bar controller to the reference you just created and select "Relationship Segue > view controllers" from the context menu that appears.按住 Ctrl 键从标签栏控制器拖动到您刚刚创建的引用,然后从出现的上下文菜单中选择“Relationship Segue > view controllers”。
  3. The tab bar should now show a square image with the title "Item" beside it.标签栏现在应该显示一个方形图像,旁边带有标题“项目”。 Click and drag this item to rearrange it in the bar as desired.单击并拖动该项目以根据需要在栏中重新排列它。
  4. In the target view controller (which should be the first responder in the referenced storyboard), create a Tab Bar Item and set the Title and Image properties in the Bar Item section of the properties panel.在目标视图控制器(应该是引用的故事板中的第一响应者)中,创建一个 Tab Bar Item 并在属性面板的 Bar Item 部分中设置TitleImage属性。

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:如果它仍然没有出现,这里有一个可行的黑客解决方法:

  1. Create a new class which inherits from UITabBarController .创建一个继承自UITabBarController的新类。
  2. 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 } }
  3. Change the type of your tab bar controller to the subclass you created (from the Identity panel in the storyboard editor).将标签栏控制器的类型更改为您创建的子类(从故事板编辑器中的身份面板)。

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.

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