0

在我的应用程序中,我创建了一个自定义 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;

请帮忙。

谢谢,

4

1 回答 1

0

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

于 2013-01-24T04:35:17.403 回答