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

标签 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];
}

最佳答案

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

而且,我发现了一个奇怪的点,看起来是一个不正常的用法。我猜你的根 viewcontroller 是一个 UITabBarController。但是, Root View Controller 被 return YES 后创建的 UINavigationController 覆盖。是故意的吗?

如果您想使用 UITabBarController 作为 Root View Controller ,请删除有关设置 UINavigationController 的代码。

关于ios - iOS 中的导航栏更改为默认颜色,而不是设置为的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21953880/

相关文章:

ios - 如何在iOS中从String的服务器响应中以数字格式显示新行

ios - 如何使用 Swift 以编程方式创建 UILabel?

ios - Google Plus 按钮在 Swift iOS 中不起作用

ios - 在 Mac OS X 和 iOS 上查找本地 IP 地址的接口(interface)/路由?

objective-c - 如何通过对象参数对 NSMutableArray 进行排序

ios - 使用导航栏返回时防止在 View Controller 中重置用户字段

ios - iPhone - 调整图像和两个标签以适应屏幕

ios - 使用 CoreData 在 TableView 顶部插入新行

ios - 拉联系电话号码问题

ios - 从网络下载图像时如何找到 "UIImagePickerControllerOriginalImage"和 "UIImagePickerControllerReferenceURL"