【问题标题】:Individual UINavigationBars for each view每个视图的单独 UINavigationBars
【发布时间】:2013-01-16 11:46:40
【问题描述】:

我有一个navigation view controllerroot view controller,我在其中设置了导航栏的外观。它看起来像这样:

UIImage *navBarImage = [UIImage imageNamed:@"GreyUp.png"];
[[UINavigationBar appearance] setBackgroundImage:navBarImage
                                       forBarMetrics:UIBarMetricsDefault];

不过,这件事改变了以下所有视图控制器的navBar 外观,这是我不喜欢的。我想要一个navBar 用于根视图,另一个用于不同的视图。如何实现?

【问题讨论】:

标签: iphone ios objective-c uinavigationcontroller uinavigationbar


【解决方案1】:

外观意味着它适用于您的所有应用导航栏,因此您可以将其添加到您的所有视图控制器中

self.navigationController.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GreyUp.png"]];

【讨论】:

    【解决方案2】:

    是的,可以在视图中使用它来实现

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar.png"] forBarMetrics:UIBarMetricsDefault];
    

    // 用于自定义后退按钮

    UIButton*    btnBack= [Utils createButtonWithTarget:self selector:@selector(btnBackClicked) frame:CGRectMake(0,5, 100, 40) bgimageName:@"tab_back.png" tag:0];//My method use yours
            self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:btnBack];
          //use btnBack ref to change font title title color etc
    

    我的 Utils 类代码来创建按钮

    + (UIButton *)createButtonWithTarget:(id)target
                                selector:(SEL)selector
                                   frame:(CGRect)frame
                             bgimageName:(NSString *)imageName
                                     tag:(NSInteger)aTag{
        UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
        button.frame = frame;
        [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];   
        [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
        // in case the parent view draws with a custom color or gradient, use a transparent color
        button.backgroundColor = [UIColor clearColor];
        button.showsTouchWhenHighlighted = YES;
        button.tag = aTag;
        return [button autorelease];
    }
    

    【讨论】:

    • 还需要自定义后退按钮吗?
    • 是的,自定义后退按钮会很棒!
    【解决方案3】:

    您可以为不同的视图设置导航栏

     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"login.png"] forBarMetrics:UIBarMetricsDefault];  
    

    导航栏上的获取按钮,您可以按照我的答案进行操作

    How to set a picture programmatically in a NavBar?

    【讨论】:

      【解决方案4】:

      在rootViewController的viewWillAppear中,自定义你的navigationBar。

      在 rootViewController 的 viewWillDisAppear 中,执行您想要为导航层次结构的其余部分显示的更改..

      【讨论】:

        【解决方案5】:

        为您的导航栏设置背景颜色或使用外观方法。您想在 viewWillAppear 中更改导航栏图像的位置

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-08-11
          • 1970-01-01
          • 2021-07-28
          • 1970-01-01
          • 2019-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多