UITabBar appearance setSelectionIndicatorImage does not work on first launch iOS7 - ios

I have a customised UITabBar and use the following code in the AppDelegate:
- (void)tabBarController:(MainUITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[self customizeTabBar];
}
- (void)customizeTabBar {
NSLog(#"*******customizeTabBar*******");
UIImage *tabBackground = [[UIImage imageNamed:#"unselectedtab"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set background for all UITabBars
[[UITabBar appearance] setBackgroundImage:tabBackground];
// Set tint color for the images for all tabbars
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
// Set selectionIndicatorImage for all tabbars
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selectedtab"]];
}
- (void)tabBarController:(MainUITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
NSLog(#"*******didEndCustomizingViewControllers*******");
}
This is all fine in iOS5+ but in 7 on first load the first TabBarItem the item indicator is white and the button seems to have been selected but the "selectedTab" image is not loaded.
When I press another tab the new tab is red and appears correctly - as does the first or any tab bar item selected after this - it only doesn't work on first launch.
customizeTabBar get called but the selected image does not appear on first launch.
didEndCustomizingViewControllers does not seem to get called at all.
This doesn't work in emulator or device on iOS7 - but does on iOS5, 6.
Any ideas?
Thanks in advance.

Setting the selection indicator image for the tab bar directly once again, apart from doing it via appearance, worked for me!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
....
UITabBarController *tabBarContr = (UITabBarController *)self.window.rootViewController;
...
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tab_bar_selection_indicator.png"]];
// iOS7 hack: to make selectionIndicatorImage appear on the selected tab on the first app run
[[tabBarContr tabBar] setSelectionIndicatorImage:[UIImage imageNamed:#"tab_bar_selection_indicator.png"]];
return YES;
}

I am seeing this exact same issue. Here is my didFinishLaunching
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self applyStyleSheet];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.backgroundColor = [UIColor redColor];
self.window.tintColor = [UIColor whiteColor];
UITabBarController *tabBarController = [self setupTabBarController];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Here is how I setup the tab bar:
- (UITabBarController *)setupTabBarController
{
UITabBarController *tabBarController = [[UITabBarController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:[[SecondViewController alloc] init]];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:[[ThirdViewController alloc] init]];
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:[[FourthViewController alloc] init]];
UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:[[FifthViewController alloc] init]];
[tabBarController setViewControllers:#[nav1, nav2, nav3, nav4, nav5]];
return tabBarController;
}
And finally, this is the tab bar customization block:
- (void)applyStyleSheet
{
UITabBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]];
[tabBar setTintColor:[UIColor whiteColor]];
[tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"tab-selected"]];
[tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}
As stated, the "tab-selected" image is not loaded on the first tab. However, I added the following line after [self.window makeKeyAndVisible] so that my tab starts up with a different tab opened, and the "tab-selected" image does show up on this tab:
[tabBarController setSelectedIndex:1];
So here's my finalized didFinishLaunching with the subtle hack that makes it work :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self applyStyleSheet];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.backgroundColor = [UIColor redColor];
self.window.tintColor = [UIColor whiteColor];
UITabBarController *tabBarController = [self setupTabBarController];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
[tabBarController setSelectedIndex:1];
[tabBarController setSelectedIndex:0];
return YES;
}

ok.
not the best of fixes but hey have to submit.
Remove the customisation code in the appdelegate and in the projects xib file (is an old project) on the TabBars attributes inspector (using xcode 5) - add the tab bars background and selection images.
This works for ios7 without the need for any of the customisation code in the appdelegate.
For pre iOS5 + 6 (this app only supports 5+) however we still need the code so I added a simple check for version and kept the code as is:
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
if(SYSTEM_VERSION_LESS_THAN(#"7.0"))
{
UIImage *tabBackground = [[UIImage imageNamed:#"unselectedtab"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set background for all UITabBars
[[UITabBar appearance] setBackgroundImage:tabBackground];
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
// Set tint colour for the images for all tabbars
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
// Set selectionIndicatorImage for all tabbars
[[UITabBar appearance] setSelectionIndicatorImage:nil];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selectedtab.png"]];
}

I think I also have had the same problem when doing my design for the new App in iOS 7!!
iOS 7 has been built more of the stuff different as we all were used to things different.
Here as I have understood we all were using StoryBoards, and were unable to integrate that Segues in our Code! :)
So I choose not to mess with the code, after I tried most of all the StackOverFlow Answers regarding this! :) Because, why you wanna do so, when you have given a Goody Good Interface Builder (IB) and Story Boarding Tool?
Question:
When we have set our Selected Tab Image, background image specially for the tab bar, it doesn't shows which tab is selected with the image we have set in our code...???
Solution
Following are the screenshots of my StoryBoard Settings I did to solve this problem!
Select your TabBarController from your via document outline panel:
Set your settings for the Tab Bar from the Utilities Panel:
Then your Program is set up to run! It now knows that first tab is selected when the App first shows the First Tab View and also which image should be shown for all the Tab Bar indicators when each of them are selected! :)
hope you all got a clue!!!
If I helped you I'm Happy!!!
But if I have wasted your Time I'm So Sorry!!! :(
But trust me, This worked me like a charm!!!

- (void)customizeTabBar {
UIImageView *customizeTabBar = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,50)];
customizeTabBar.image=[UIImage imageNamed:#"Tab_bar.png"];
firstTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab1.png"] highlightedImage:[UIImage imageNamed:#"tab11.png"]];
[firstTab setFrame:CGRectMake(8.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: firstTab];
secondTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab2"] highlightedImage:[UIImage imageNamed:#"tab22"]];
[secondTab setFrame:CGRectMake(115.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: secondTab];
thirdTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab3"] highlightedImage:[UIImage imageNamed:#"tab33"]];
[thirdTab setFrame:CGRectMake(223.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: thirdTab];
self.tabBar.tag=10;
[self.tabBar addSubview:customizeTabBar];
}

Related

UITabBarController initially highlights all tab bar item images

I'm programmatically instantiating a UITabBarController that will manage 2 view controllers, and then setting it as the rootViewController. However when the views appear, it highlights all the tab item's images (although the text is properly highlighted). No matter what I set to the selectedIndex the images will all appear highlighted. Only when you tap on the tab bar items does it actually toggle the highlighted state on the images. What's going on here?
Code:
UITabBarController *tabController = [[UITabBarController alloc] init];
UIStoryboard *storyboard = [self storyboard];
OGVideoStreamViewController *questionsController = [storyboard instantiateViewControllerWithIdentifier:#"OGVideoStreamViewController"];
questionsController.isQuestion = YES;
OGVideoStreamViewController *answersController = [storyboard instantiateViewControllerWithIdentifier:#"OGVideoStreamViewController"];
answersController.isQuestion = NO;
OGMatchesViewController *matchesController = [[OGMatchesViewController alloc] initWithNibName:#"OGMatchesViewController" bundle:nil];
questionsController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Questions" image:[UIImage imageNamed:#"Tab Icon - Questions"] tag:0];
answersController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Answers" image:[UIImage imageNamed:#"Tab Icon - Answers"] tag:1];
matchesController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Inbox" image:[UIImage imageNamed:#"Tab Icon - Inbox"] tag:2];
[tabController setViewControllers:#[questionsController, answersController, matchesController] animated:NO];
tabController.selectedViewController = questionsController;
[[UIApplication sharedApplication] keyWindow].rootViewController = tabController;
I've had the same problem and it was due to me calling:
[UIView appearance].tintColor = [UIColor colorWithRed:1.000
green:0.793
blue:0.236
alpha:1.000];
I called it in my AppDelegate, so i could change it to the following:
self.window.tintColor = [UIColor colorWithRed:1.000
green:0.793
blue:0.236
alpha:1.000];

How to display all tabBar titles in ios7

Basically i cant get to display all tabBar Items when i run my app, just the first view controller is displayed:
I literally have to click on a tab to display its Item:
This my code in Appdelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Initialize window
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
// Set background colors for both NavBar and TabBar
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.157 green:0.718 blue:0.553 alpha:1]];
[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:0.141 green:0.216 blue:0.263 alpha:1]];
// Initialize your five tab controllers. with each tab has its own navigation controller
HomePageView *homePageView = [[HomePageView alloc]init];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:homePageView];
ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:profileViewController];
FeedViewController *feedViewController=[[FeedViewController alloc]init];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:feedViewController];
ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:listeningSessionViewController];
RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];
UINavigationController *nav5 = [[UINavigationController alloc]initWithRootViewController:recievedViewController];
// initialize tabbarcontroller,set your viewcontrollers and change its color.
self.tabC = [[UITabBarController alloc]init];
NSArray* controllers = [NSArray arrayWithObjects: nav1,nav2,nav3,nav4,nav5, nil];
[self.tabC setViewControllers: controllers animated:NO];
[_window addSubview:self.tabC.view];
// Show window
[self.window makeKeyAndVisible];
return YES;
}
I'm guessing that you're setting the titles in the viewDidLoad or viewDidAppear methods of the controllers. That won't work, because, while all the controllers are instantiated in the app delegate, only the controller at index 0 has its view loaded, and thus viewDidLoad will not be run for the other controllers. Instead, you should set the titles on the navigation controllers in the app delegate,
ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:profileViewController];
nav2.tabBarItem.title = #"Profile";

MPMediaPickerController customize the colors of the labels and icons

I need change the labels colors of MPMediaPickerController item, on iOS 7 , im using Xcode5, what can i do?.
My code of the creation of MPMediaPickerController is:
- (IBAction)addSongs:(id)sender {
MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
}
I was trying with this but not works: https://gist.github.com/acidlemon/1955332
Examples:
Thanks in advance.
Change your tab bar's tintColor using the appearance proxy, e.g.:
[[UITabBar appearance] setTintColor:[UIColor yellowColor]];
To change the tintColor of all views, including your label, you can do the following in your AppDelegate.m file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIView appearance] setTintColor:[UIColor yellowColor]];
return YES;
}

Strange offset on right bar button item

I encountered a strange problem on the rightBarButtonItem of UINavigationController. The margin of the button disappears on iOS7, so it looks like this:
At first, I thought it was some mistakes in my UINavigationController related categories, but I removed all my customisation code and all the header files of categories, and simply use a UINavigationController and an empty view controller.
In - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
VCTestViewController *vc = [[[VCTestViewController alloc]init]autorelease];
UINavigationController *nc = [[[UINavigationController alloc]initWithRootViewController:vc]autorelease];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
In VCTestViewController.m -> viewDidLoad
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"hello" style:UIBarButtonItemStyleBordered target:nil action:nil];
The problem still exists(as shown above). I can't figure out what is wrong. Do you have any idea about this problem?
you can share your line of code if it's not working:
- (UIBarButtonItem *)rightNavBarButton
{
UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[filterBtn setTitle:#"hello" forState:UIControlStateNormal];
filterBtn.frame = CGRectMake(0, 0,40,27);
[filterBtn addTarget:self action:#selector(getFriendsList) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *filterNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:filterBtn];
return filterNavBarItem;
}
I finally figure out what happened.
The link given by βhargavḯ is not the solution of my problem, but it's the cause of my problem. One of my colleagues wrote a category of UINavigationItem to narrow the empty space in iOS7. He then used method swizzling to exchanged the system method setRightBarButtonItem: with his own method. I didn't realise this.
So I think method swizzling would be extremely dangerous if you exchange the system methods with your own methods.

ABUnknownPersonVIewController and NavigationBarColor

I've converted an app from ios6 to ios7. The app sets the background color of the navigationbar in the following way.
[[UINavigationBar appearance] setBackgroundColor: [UIColor blueColor]];
This works fine almost everywhere. But when I use the ABUnknownPersonViewController and click "create new contact" or "add to existing contact" the header is white on white background.
Is there any way to change the navbar background color of this view?
The view is initiated like this
ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];
picker.unknownPersonViewDelegate = self;
picker.displayedPerson = aContact;
picker.allowsAddingToAddressBook = YES;
picker.allowsActions = YES;
picker.alternateName = self.contact.fullName;
picker.title = self.contact.fullName;
[self.navigationController pushViewController:picker animated:YES];
The first view looks fine with the custom header/background color. It's when you click "add" or "create" that the header goes white.
Thanks
Johan
Try
[[UINavigationBar appearance] setBarTintColor: [UIColor blueColor]];
try like this....
ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];
UINavigationController *newNavigationController = [[UINavigationController alloc] initWithRootViewController:picker];
// customise navigation bar of navigation controller
[self presentModalViewController:newNavigationController animated:YES];

Resources