如何根据要求在每个其他控制器中更改 NavigationController 的 NavigationBar?

Posted

技术标签:

【中文标题】如何根据要求在每个其他控制器中更改 NavigationController 的 NavigationBar?【英文标题】:How to change NavigationBar of NavigationController in every other controller based on requirement? 【发布时间】:2013-01-23 19:13:39 【问题描述】:

在我的应用程序中,我创建了一个自定义 UINavigationBar,在其中我编写了不同的方法来自定义 UINavigationBar,例如带有标题图像、后退按钮和签核按钮的导航栏,其中一些将只有其中两个,其中一些只有标题图像. 我必须根据我们所在的视图控制器更改导航栏。

这是我的 CustomUINavigationClass

#import <UIKit/UIKit.h>

typedef enum 
    simple = 1,
    back,
    signoff,
    both
 NavBarChoices;


NavBarChoices optionSelect;

@interface CustomNavigationBar : UINavigationBar

    UIBarButtonItem * menuButton;
    UIButton * showEditButton;


- (id)initWithFrame:(CGRect)frame andOption:(NavBarChoices)choice;
@end

.m

#import "CustomNavigationBar.h"

@implementation CustomNavigationBar

- (id)initWithFrame:(CGRect)frame andOption:(NavBarChoices)choice

    self = [super initWithFrame:frame];
    if (self) 
        // Initialization code
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CustomNavigationBar"
                                                              owner:self
                                                            options:nil];

        if ([arrayOfViews count] < 1)
            return nil;
        
        optionSelect = choice;
        CustomNavigationBar *newView = [arrayOfViews objectAtIndex:0];

        self = newView;

    
    return self;



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect


    switch (optionSelect) 
        case 1:
            [self drawSimpleNavBar];
            break;
        case 2:
            [self drawWithBackOnly];
            break;
        case 3:
            [self drawWithSignOffOnly];

            break;
        case 4:
            [self drawWithBackAndSignOff];
            break;

        default:
            break;
    



-(void)drawSimpleNavBar 

    self.tintColor = [UIColor whiteColor];
    self.backgroundColor = [UIColor whiteColor];
    UIImage *image = [UIImage imageNamed:@"title_logo.png"];
    [image drawInRect:CGRectMake(100, 13, 104, 20)];

    UIImage *menuImage = [UIImage imageNamed:@"menu_off.png"];
    showEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
    showEditButton.bounds = CGRectMake(0, 0, menuImage.size.width, menuImage.size.height+10);
    showEditButton.frame = CGRectMake(2, 10, menuImage.size.width, menuImage.size.height);
    [showEditButton setImage:menuImage forState:UIControlStateNormal];
    [showEditButton setImage:[UIImage imageNamed:@"menu_on.png"] forState:UIControlStateSelected];
    [showEditButton addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:showEditButton];



-(void)drawWithBackOnly 

    self.tintColor = [UIColor whiteColor];
    self.backgroundColor = [UIColor whiteColor];
    UIImage *image = [UIImage imageNamed:@"title_logo.png"];
    [image drawInRect:CGRectMake(100, 13, 104, 20)];

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];

    [backButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 3, 80, 30);
    backButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    [backButton sizeToFit];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backButton];



-(void)drawWithSignOffOnly 




-(void)drawWithBackAndSignOff 



-(void)showMenu 
    NSLog(@"Clicked");
    showEditButton.hidden = YES;
    [showEditButton setImage:[UIImage imageNamed:@"menu_on.png"] forState:UIControlStateNormal];



@end

我在 App Delegate 中这样称呼它:

navigationBar = [[CustomNavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44) andOption:simple];
    UINavigationController * nav = [[UINavigationController alloc]initWithNavigationBarClass:[navigationBar class] toolbarClass:nil];

    self.viewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    [nav setViewControllers:@[_viewController] animated:NO];

self.window.rootViewController = nav;

现在我在第一个视图控制器中有一个按钮,单击时我想推送另一个视图控制器,它将向 CustomNavigationBar 类发送请求以更改同一 NavigationController 的导航栏。

请指导,目前我正在尝试更改新控制器的 viewDidLoad 方法,但挑战是 UINavigationController 的 NavigationBar 属性是只读的,因此无法为其分配新的导航栏,否则我会做这样的事情

CustomNavigationBar * navBar = [[CustomNavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44) andOption:2];
self.navigationController.navigationBar = navBar;

请帮忙。

谢谢,

【问题讨论】:

【参考方案1】:

您是否在 xib 实用程序中定义了 NIB 和类名称?如果是这样,请确认类名和 NIB 名称是否相同,希望它能在这种情况下工作

【讨论】:

以上是关于如何根据要求在每个其他控制器中更改 NavigationController 的 NavigationBar?的主要内容,如果未能解决你的问题,请参考以下文章

如何在每个带有 codedeploy 的代码中更改 AMI 以实现自动缩放?

如何更改不同分段控制索引的数据源

如何使用子视图控制器中的按钮单击更改 tabBar 项目标题

如何根据用户组更改 Django 模板?

在 React-native 中,如何更改 NavigatorIOS 的样式

如何在 Grails 3 中更改每个插件的编解码器?