iOS中的导航栏更改为默认颜色,而不是设置为的颜色

时间:2014-02-22 11:48:09

标签: ios objective-c

在我实现访问地址簿的请求之前,我的导航栏设置为某种颜色,但是一旦我实现了请求,导航栏颜色就变成了默认的灰色。我试图重新安排代码,但它不会显示地址簿请求。

- (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];
}

1 个答案:

答案 0 :(得分:3)

我在代码中间找到了return YES。返回代码放在设置导航栏外观的代码之前。我认为这是你正在寻找的问题。将return YES放在方法的末尾。

并且,我发现了一个奇怪的观点,看起来像是异常使用。我猜你的根视图控制器是一个UITabBarController。但是,根视图控制器被return YES之后的UINavigationController覆盖。这是故意的吗?

如果要使用UITabBarController作为根视图控制器,请删除有关设置UINavigationController的代码。