简体   繁体   English

iOS启动情节提要:如何更改选项卡栏的色调颜色?

[英]iOS Launch Storyboard: How can I change a Tab Bar's tint color?

What I'm doing: 我在做什么:

Using a 'launch storyboard'. 使用“启动故事板”。 It's quite simple, and contains a default UITabBarController. 这很简单,并且包含一个默认的UITabBarController。 I've set the tab bar's 'tintColor' to red in the launch storyboard, as well as in my app. 我已经在启动情节提要板和我的应用程序中将标签栏的“ tintColor”设置为红色。 I'm using Xcode 7, iOS 9. 我正在使用Xcode 7,iOS 9。

What doesn't work: 什么不起作用:

The launch screen loads the tab bar using the default blue iOS tint color...! 启动屏幕使用默认的蓝色iOS色调颜色加载选项卡栏...! Then after loading, the tint color switches to red when the launch screen storyboard is replaced. 然后,在加载后,更换启动屏幕情节提要板时,颜色颜色将变为红色。


How on earth are you meant to set a tab bar's tint color in a storyboard? 您到底打算在情节提要中设置标签栏的颜色是什么意思?

Demo Project: http://s000.tinyupload.com/?file_id=73998115878034693063 演示项目: http//s000.tinyupload.com/?file_id = 73998115878034693063

The right way to go is to confuse Xcode. 正确的方法是混淆Xcode。 Xcode should not know that the launch storyboard that is used is actually a launch storyboard. Xcode不应该知道所使用的启动情节提要实际上是一个launch情节提要。 This way you would be able to set some custom User Defined Runtime Attributes . 这样,您将能够设置一些自定义的User Defined Runtime Attributes However, you still would NOT be able to run some custom code... 但是,您仍然无法运行某些自定义代码...

So... To do this, follow these steps: 所以...为此,请按照下列步骤操作:

  1. Create a new Xcode project 创建一个新的Xcode项目

  2. Copy your Main.storyboard into desktop and rename it to CustomLaunchScreen.storyboard . 您复制Main.storyboard到台式机和其重命名为CustomLaunchScreen.storyboard

  3. Add this CustomLaunchScreen.storyboard to the project. 将此CustomLaunchScreen.storyboard添加到项目中。

  4. Open your Info.plist file and change key Launch screen interface file base name value from LaunchScreen to CustomLaunchScreen . 打开你的Info.plist file并更改键Launch screen interface file base name值从LaunchScreenCustomLaunchScreen

  5. Open your CustomLaunchScreen.storyboard . 打开您的CustomLaunchScreen.storyboard Delete the default UIViewController and set the UITabBarController as your initial view controller . 删除默认的UIViewController并将UITabBarController设置为您的initial view controller

  6. Open the tabBar property of your UITabBarController and navigate to User Defined Runtime Attributes 打开UITabBarControllertabBar属性,然后导航到“ User Defined Runtime Attributes

  7. Add the tintColor property, set type as Color and set some custom value. 添加tintColor属性,将type设置为Color并设置一些自定义值。

You can also watch a full video tutorial Here 您也可以在这里观看完整的视频教程

Well thanks to @OIDor for his solution, it is a great hack. 好,感谢@OIDor提供的解决方案,这是一个很棒的技巧。

To be clear however, you don't need to do all that. 需要明确的是,您无需执行所有操作。 All you have to do is: 您要做的就是:

  1. Edit the source of your Launch Screen storyboard and change launchScreen="YES" to launchScreen="NO" , this enables you to do the next part... 编辑启动屏幕情节launchScreen="YES"的源,并将launchScreen="YES"更改为launchScreen="NO" ,这使您可以进行下一部分...
  2. Add the user-defined attribute tintColor on the Tab Bar in the storyboard. 在情节tintColor的选项卡栏上添加用户定义的属性tintColor This is not permitted by Xcode without the first step 没有第一步,Xcode不允许这样做

Hey presto it's all working. 嘿,这一切正常。

I think I've figured this out. 我想我已经知道了。

There are quite a few blog posts online about how Launch Storyboards work, but not a lot from Apple. 在线上有很多关于Launch Storyboards如何工作的博客文章,但是Apple并没有很多。 I found from this blog post which discussed that the launch screen images are captured at runtime from the launch storyboard. 我从这篇博客文章中发现, 该文章讨论了启动屏幕图像是在运行时从启动情节提要中捕获的。 Digging into my simulator directory revealed that they were in fact there: 进入我的模拟器目录,发现它们实际上在那里:

发射1

I'm not sure why, but the call to set the UITabBar 's tintColor does not fire in time when the launch storyboard has loaded, but setting colors for other values such as the barTintColor property works: 我不确定为什么,但是在加载启动barTintColor板后,设置UITabBartintColor的调用不会及时触发,但是可以为其他值(如barTintColor属性)设置颜色:

launch2

All I can say is this: setting the tintColor on a UITabBar on a view controller in a launch storyboard is not supported. 我只能说的是:不支持在启动情节UITabBar的视图控制器的UITabBar上设置tintColor There is something going on behind-the-scenes when the launch storyboard is loaded preventing this from working. 加载启动情节提要板时,后台发生了某些事情,阻止了此工作。

TL;DR: you can't do this with a Launch Storyboard. TL; DR:您无法使用Launch Storyboard来做到这一点。 For a workaround that tricks Xcode into treating your launch storyboard as a regular storyboard, see OlDor's answer . 有关使Xcode 诱使您将启动情节提要板视为常规情节提要板的变通办法,请参见OlDor的答案

As an alternative, you could take a screenshot of your app with the tab bar loaded properly, tweak it, and use that in a UIImageView on your launch view controller. 作为替代方案,您可以捕获应用程序的屏幕快照,并正确加载选项卡栏,对其进行调整,然后在启动视图控制器上的UIImageView使用该屏幕快照。

If you want to add just the tab bar without any currently selected tabs, add a UITabBar to your launch UIViewController and style the UITabBarItem s to your liking: 如果您只想添加选项卡栏而没有当前选中的选项卡,则将UITabBar添加到启动UIViewController并根据喜好设置UITabBarItem的样式:

标签

This UITabBar has two UITabBarItems which can be styled without adding outlets to their view controllers. UITabBar具有两个UITabBarItems ,可以对其进行样式设置,而无需在其视图控制器中添加插座。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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