UITabBar acts strange in iOS 7, how to fix it? - ios

[[UITabBar appearance] setTintColor:[UIColor redColor]]; // for unselected items that are red
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]]; // for selected items that are green
Why is this code not working in iOS 7?
setTintColor works but only changes the "selected"-icon's color, not the unselected ones as it did in earlier iOS versions, which is weird ? setSelectedImageTintColor does'nt work at all anymore ? Is it realy not possible to color icons as you wish anymore?
Also the setSelectionIndicatorImage is not working as intended in the start of the app, what is happening in iOS 7?
Derp herp Apple, why ?

As of iOS 7 you have to use setBarTintColor: to set the background color, with setTintColor: now affecting the foreground color.

Answer from Adam Waite doesn't work. Method setFinishedSelectedImage under iOS7 ignores the selected image. You need to so it like this:
UITabBarItem *item1 = _tabBarController.tabBar.items[0];
item1.image = [[UIImage imageNamed:#"item1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item1.selectedImage = [UIImage imageNamed:#"item1-selected"];

It's not tint, but you can do it with images:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"item_unselected.png"]];

This is a known issue in iOS 7. The tintColor is used for the selected tab image. The selectedImageTintColor is completely ignored. There is no way to tint unselected tab images.
See a discussion on the Apple Developer Forums https://devforums.apple.com/message/851126#851126 about this.

for (int i=0; i (smallerthen)arrayOfTabBarItems.count; i++) {
NSString *unselectedImageName = [NSString stringWithFormat:#"%#.png", arrayOfTabBarItems[i]];
NSString *selectedImageName = [NSString stringWithFormat:#"%#-pressed.png", arrayOfTabBarItems[i]];
[tabBarCtrl.tabBar.items[i] setFinishedSelectedImage:[UIImage imageNamed:selectedImageName] withFinishedUnselectedImage:[UIImage imageNamed:unselectedImageName]];
}
This worked for me.

Related

iOS 7 uitabbar shows, invisible on ios8

I have a previously existing app (pre ios8) that uses UITabbar. The tabbar is visible in ios7 simulator and device, but it is invisible in ios8. What is causing this issue? the space for the tab bar is there, but its background and text/images are not there. i've attached a pic of it.
iOS 7:
iOS 8:
Even if setFinishedSelectedImage:withFinishedUnselectedImage: is deprecated in iOS7, it is working fine in iOS7 but not in 8.
Use image and selectedImage property of UITabBarItem instead.
I also had the same issue but my problem was different.
Reference code:
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
if ([self iOS7OrAbove])
{
//use UIImageRenderingModeAlwaysOriginal to set the custom image for ios 7 and above.
tabBarItem1.selectedImage = [[UIImage imageNamed:#"SelectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1.image = [[UIImage imageNamed:#"UnselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
else
{
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"SelectedImage"] withFinishedUnselectedImage:[UIImage imageNamed:#"UnselectedImage"]];
}

Change color of navigation bar ios6 and ios7

I'm in the process of upgrading my app to iOS7. However I want to preserve the iOS6 interface as well. It works well except for the navigation bar(s). In iOS7 they look great (just the default color with the translucent property to YES. In iOS6 the navigation bars are showing as the default blue bars and I want them to be black translucent.
What I do is check the version of the iOS and then perform some code. In the debugger I see they right version in the vComp variable but the color is not changing. Don't mind the redColor property, that's just for the test. In both ways I am presented with the default color.
Here is my code:
- (void) fixNavBarColor:(UINavigationBar*)bar {
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[vComp objectAtIndex:0] intValue] >= 7) {
bar.barTintColor = [UIColor redColor];
bar.translucent = NO;
}
else {
bar.tintColor = [UIColor redColor];
bar.opaque = YES;
}
}
There is no error or warning.
Any ideas?
You shouldn't set the tintColor straight to navigationBar as it wouldn't be applied to other parts of your app, you should instead use UINavigationBar's appearance to set tintColor which is available on iOS 5.0 onwards.
[[UINavigationBar appearance] setTintColor:"Your Color"];
Use this code for iOS6
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0 alpha:.8]]
forBarMetrics:UIBarMetricsDefault];

How to change the unselected tabbaritem color in iOS7?

Before iOS 7 I used
[[UITabBar appearance] setTintColor:[UIColor redColor]];
But now it only paint the selected item, I have read some suggestions but I can not fin how to do it, I used this too:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"openbookwp4.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"openbookwp4.png"]];
this put the icon I want, with the color I want, but only after I selected that tab for example, when I open the app the tab looks normal, but after I press the second tab and return to the first, the second tab now has the color I want. It is hard to explain without images, but I can not post images...
This code works on iOS 7.
[[UITabBarItem appearance] setTitleTextAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Bold" size:10.0f],
NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
} forState:UIControlStateNormal];
Set foreground color as you like.
To affect also the non selected tabbar icons:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], UITextAttributeTextColor, nil]
forState:UIControlStateNormal];
If it does not work the only way is with images for selected and unselected states:
// set selected and unselected icons
UITabBarItem *item = [self.tabBar.items objectAtIndex:0];
// this way, the icon gets rendered as it is (thus, it needs to be green in this example)
item.image = [[UIImage imageNamed:#"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:#"selected-icon.png"];
In my case the problem was that I defined the tab bar items in viewDidLoad only. If you do this, it is clear that the images are only set once the view of the corresponding tab has been loaded, but not at first (when only the first tab is selected).
My solution was to define the custom tab items in the view controller's init method, then the unselected images are visible even when the controller's view has not been loaded yet.
Following on from Nikos Answer
For swift 2.* it would look like this
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:.Selected)
let Item1 = self.items![0]
Item.image = UIImage(named: "Icon1")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let Item2 = self.items![1]
Item2.image = UIImage(named: "Icon2")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let Item3 = self.items![2]
Item3.image = UIImage(named: "Icon3")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

How do I use iOS7 controls without upgrading your nib files?

At this point in time I'd like to keep my code compatible with Xcode 4, therefore I don't want to upgrade my nib files.
However, I can't change the UISegmentedControl control tint colour and use the new iOS7 version of the control without upgrading the nib file.
I've seen ..
[NSUserDefaults standardUserDefaults] setObject:#Yes forKey#"UIUseLegacyUID"]
Which will keep iOS6 layout, I'm not sure if there is an future version or whether this would help?
With Xcode 5, you can change your XIB to use the property of Xcode 4 and iOS 6.
By default you have:
But, you can change the property for Xcode 4.6 and iOS 6:
You can change the tint color in the code like this:
UISegmentedControl *segmentedControl = ...;
// For backwards compatibility
if ([segmentedControl respondsToSelector:#selector(tintColor)])
{
segmentedControl.tintColor = [UIColor redColor];
}
You can set different segment image and color for each segment. For color you may use:
//get the subviews of the segmentedcontrol
NSArray *arri = [segmentedControl subviews];
//change the color of every subview(segment) you have
[[arri objectAtIndex:0] setTintColor:[UIColor redColor]];
[[arri objectAtIndex:1] setTintColor:[UIColor greenColor]];

Customizeappearance compatibility on others iOS

I did not see an answer to my question yet so I ask it :
I am currently using (void)customizeappearance to customize the design of my tabbar and navbar. Will that function restrain the use of my app to iOS5-based iPhone only ? If not, will my bars be seen the same in all devices ?
Thanks in advance.
Here is the method I am using :
- (void)customizeAppearance
{
UIImage *tabBackground = [[UIImage imageNamed:#"bg_tab"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
[[UITabBar appearance] setSelectionIndicatorImage:
[UIImage imageNamed:#"bg_tab_selected"]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
}
customizeappearance isn't a standard method. What methods are you actually calling to do the customisation?
If you are using methods like setBackgroundImage: or setTintColor: there's a good chance that your app will only run on iOS5 and will crash on iOS4. Post the methods you are using and I'll show you how to do it safely for iOS4.
Here's another answer that explains how to safely call iSO5-only customisation methods so that they won't crash on iOS4: iOS change tabbar item color is safe?
Here's an example of how to customise in a way that works on iOS 4 & 5: tabbar item image and selectedImage

Resources