Top
Top

Reputation: 61

Navigation Bar in iOS changed to default colour rather than colour which it was set to

Before i implemented the request to access the address book my navigation bar was set to a certain colour, but once I implemented the request the navigation bar colour turned into the default grey colour. I tried to rearrange the code but it then would not show the address book request.

- (void) displayMessage:(NSString *)paramMessage
{
    [[[UIAlertView alloc] initWithTitle:nil
                                message:paramMessage
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Address book
    CFErrorRef error = NULL;

    switch (ABAddressBookGetAuthorizationStatus()){
        case kABAuthorizationStatusAuthorized:{
            addressBook = ABAddressBookCreateWithOptions(NULL, &error);
            // Do your work and once you are finished ... //
            if (addressBook != NULL){
                CFRelease(addressBook);
            }
            break;
        }
        case kABAuthorizationStatusDenied:{
            [self displayMessage:kDenied];
            break;
        }
        case kABAuthorizationStatusNotDetermined:{
            addressBook = ABAddressBookCreateWithOptions(NULL, &error);
            ABAddressBookRequestAccessWithCompletion
            (addressBook, ^(bool granted, CFErrorRef error) {
                if (granted){
                    NSLog(@"Access was granted");
                } else {
                    NSLog(@"Access was not granted");
                }
                if (addressBook != NULL){
                    CFRelease(addressBook);
                }
            });
            break;
        }
        case kABAuthorizationStatusRestricted:{
            [self displayMessage:kRestricted];
            break;
        }
    }



    //Assign tab bar item with titles
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
    UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];

    tabBarItem1.title = @"Tab1";
    tabBarItem2.title = @"Tab2";
    tabBarItem3.title = @"Tab3";
    tabBarItem4.title = @"Tab4";

    [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"Tab1selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Tab1.png"]];
    [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"Tab2selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Tab2.png"]];
    [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"Tab3selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Tab3.png"]];
    [tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"Tab4selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Tab4.png"]];
    return YES;

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    // Change the tab bar background
    UIImage *tabBarBackground = [UIImage imageNamed:@"CustomUITabbar.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    [[UITabBar appearance] setTintColor:[UIColor colorWithRed:(106/255.0) green:(200/255.0) blue:(0/255.0) alpha:1]];

    //Custom Navbar
    UIImage *navbar = [UIImage imageNamed:@"navbar.png"];
    [[UINavigationBar appearance] setBackgroundImage:navbar forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil];
    [[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
}

Upvotes: 0

Views: 166

Answers (1)

Kyokook Hwang
Kyokook Hwang

Reputation: 2762

I found return YES in the middle of your code. the return code is placed before the code that sets appearances of the navigation bar. I think that is the problem you are looking for. place return YES at the end of the method.

And, I found a weird point that looks like a abnormal usage. I could guess that your root viewcontroller is a UITabBarController. But, the root viewcontroller is overridden by UINavigationController made after return YES. Is it intentional?

If you want to use UITabBarController as the root viewcontroller, remove the codes about setting UINavigationController.

Upvotes: 3

Related Questions