iOS Launch Storyboard: How can I change a Tab Bar&

2019-06-18 07:22发布

问题:

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:

  1. Create a new Xcode project

  2. Copy your Main.storyboard into desktop and rename it to CustomLaunchScreen.storyboard.

  3. Add this CustomLaunchScreen.storyboard to the project.

  4. Open your Info.plist file and change key Launch screen interface file base name value from LaunchScreen to CustomLaunchScreen.

  5. Open your CustomLaunchScreen.storyboard. Delete the default UIViewController and set the UITabBarController as your initial view controller.

  6. Open the tabBar property of your UITabBarController and navigate to User Defined Runtime Attributes

  7. Add the tintColor property, set type as Color 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:

  1. Edit the source of your Launch Screen storyboard and change launchScreen="YES" to launchScreen="NO", this enables you to do the next part...
  2. 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 UITabBarItems to your liking:

This UITabBar has two UITabBarItems which can be styled without adding outlets to their view controllers.