简体   繁体   中英

Bar button items tint color ignored in iOS 7

I'm working on an iOS 6 app that's needs to be compatible with iOS 7. I use UIAppearance to style the tint color on some of the components of the app, in particular the UINavigationBar and its Bar Button items.

The problem is that when I deploy the app on an iOS 7 device, the bar button items' tint color is modified to the color of the navigation bar. I understand that iOS 7's UIAppearance introduces some modifications in this ( UINavigationBar ) and that if I want to use it properly for iOS 7 I should set the barTintColor property instead, however since I mean to keep my app as iOS 6 and I'm compiling it with SDK 6 in Xcode 4.6.3 there's no way I can use that property.

I've tried many things, some of them worked in some scenarios but I still can't get it to work in the whole app. Any ideas?

Here's a screenshot of the problem I'm having

在此输入图像描述

EDIT 1

To style the app I added the following code in the AppDelegate

[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0]];
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:230.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0]];

EDIT 2

I tried to deploy the app from Xcode 5 with SDK 7 and deployment target 5, but when I deployed it on an iOS 7 device it changed my UI to iOS 7. This is not a matter of recognising when is the app is running in iOS 7 because I don't want the app to look iOS 7 like, I want it to look the same as it looks in iOS 6

Thanks in advance!

You don't need to restrict yourself to the iOS 6 SDK in order for your app to run on iOS 6. The minimum version that your app will run on is set using the 'Deployment Target' under Deployment Info in the General tab. If you set that to iOS 6, but set your 'Base SDK' to Latest iOS under the Build Settings tab, you'll be able to write an app that can run on either.

Once you've done that, you need to check at runtime whether the features you need are available. So you might do something like this:

UIColor *navigationBarColor = [UIColor redColor];
if ([self.navigationBar respondsToSelector:@selector(setBarTintColor:)])
{
    self.navigationBar.barTintColor = navigationBarColor;
}
else
{
    self.navigationBar.tintColor = navigationBarColor;
}

You can determine, at runtime, whether you're running iOS 6 or 7, and introduce OS-specific user interface tweaks at that time.

Best way to check for iOS 7 or earlier

Specifically, to check for iOS 7 or higher:

#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

Then you can just check:

if (IS_OS_7_OR_LATER) {
    // iOS 7 tweaks
} else {
    // iOS 6 tweaks
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) 
{


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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