问题
What I'm doing:
Using a 'launch storyboard'. It's quite simple, and contains a default UITabBarController. I've set the tab bar's 'tintColor' to red in the launch storyboard, as well as in my app. I'm using Xcode 7, iOS 9.
What doesn't work:
The launch screen loads the tab bar using the default blue iOS tint color...! 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
回答1:
The right way to go is to confuse Xcode. Xcode should not know that the launch storyboard that is used is actually a launch
storyboard. This way you would be able to set some custom User Defined Runtime Attributes
. However, you still would NOT be able to run some custom code...
So... To do this, follow these steps:
Create a new Xcode project
Copy your
Main.storyboard
into desktop and rename it toCustomLaunchScreen.storyboard
.Add this
CustomLaunchScreen.storyboard
to the project.Open your
Info.plist file
and change keyLaunch screen interface file base name
value fromLaunchScreen
toCustomLaunchScreen
.Open your
CustomLaunchScreen.storyboard
. Delete the defaultUIViewController
and set theUITabBarController
as yourinitial view controller
.Open the
tabBar
property of yourUITabBarController
and navigate toUser Defined Runtime Attributes
Add the
tintColor
property, settype
asColor
and set some custom value.
You can also watch a full video tutorial Here
回答2:
Well thanks to @OIDor for his solution, it is a great hack.
To be clear however, you don't need to do all that. All you have to do is:
- Edit the source of your Launch Screen storyboard and change
launchScreen="YES"
tolaunchScreen="NO"
, this enables you to do the next part... - Add the user-defined attribute
tintColor
on the Tab Bar in the storyboard. This is not permitted by Xcode without the first step
Hey presto it's all working.
回答3:
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. 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:
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:
All I can say is this: setting the tintColor
on a UITabBar
on a view controller in a launch storyboard is not supported. 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. For a workaround that tricks Xcode into treating your launch storyboard as a regular storyboard, see OlDor's answer.
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.
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:
This UITabBar
has two UITabBarItems
which can be styled without adding outlets to their view controllers.
来源:https://stackoverflow.com/questions/35758679/ios-launch-storyboard-how-can-i-change-a-tab-bars-tint-color