我在UIViewController的子类中设置的UINavigationBar的自定义样式方面遇到麻烦。

我以前曾进行过这项工作,但不确定为什么它在这里不起作用。

我的视图控制器中有以下代码:

- (void) loadView {
    [super loadView];

    CGRect frame = self.view.bounds;

    navBar = [[UINavigationBar alloc] initWithFrame:frame];
    frame.size = [navBar sizeThatFits:frame.size];
    [navBar setFrame:frame];
    [navBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

    // The following styling has no effect:
    [navBar setBarTintColor:[UIColor blackColor]];
    [navBar setTintColor:[UIColor grayColor]];

    NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor whiteColor],NSForegroundColorAttributeName, nil];
    [[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

    UIBarButtonItem * button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
        target:self
        action:@selector(nextView:)];

    [[self navigationItem] setRightBarButtonItem:button];
    [button release];
    [navBar setItems:[NSArray arrayWithObject:self.navigationItem]];

    [self.view addSubview:navBar];

}

最佳答案

您不应该通过初始化一个新的并将其添加为子视图来弄乱UINavigationBar。而是使用UINavigationBar.appearance的可能性:

UINavigationBar.appearance.barTintColor = UIColor.blackColor;
UINavigationBar.appearance.tintColor = UIColor.grayColor;


请注意,这会更改应用程序各处的UINavigationBar的外观,因此会更改为appearance。如果这不是您想要的,可以尝试:

self.navigationController.navigationBar.barTintColor = UIColor.blackColor;
self.navigationController.navigationBar.tintColor = UIColor.grayColor;

关于ios - UINavigationBar的样式在UIViewController中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32290195/

10-13 04:00