带有自定义 NavigationBar 的 UINavigationController

我正在尝试使用自定义的 UINavigationController 创建一个 NavigationBar

我创建了一个类 (myNavBar),它继承自 UINavigationBar

我这样称呼我的 navigationController

UINavigationController *navControl = [[UINavigationController alloc] initWithNavigationBarClass:[myNavBar class] toolbarClass:[UIToolbar class]];
[navControl pushViewController:myVewController animated:NO];

但我得到这个错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot call setItems:animated: directly on a UINavigationBar managed by a controller.'

我的代码有什么问题。

更新

这是 myNavigationBar 类:

#import <UIKit/UIKit.h>

@interface myNavBar : UINavigationBar

-(void)initNavBarButtons:(UIViewController*)sender;

@end

---------------------------------------------------

#import "myNavBar.h"

@implementation myNavBar
{
    UIButton *menuButton;
    UIButton *searchButton;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Do any additional setup
    }
    return self;
}

-(void)initNavBarButtons:(UIViewController*)sender
{
    [menuButton addTarget:sender action:@selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
    [searchButton addTarget:sender action:@selector(revealSearch:) forControlEvents:UIControlEventTouchUpInside];
}

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize
{
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

-(id)init
{   self = [super init];
    return self;
}

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

    //[self setBarStyle:UIBarStyleBlack];

    menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    menuButton.frame = CGRectMake(0, 0, 25, 22);
    [menuButton setCenter:CGPointMake(25, self.center.y)];
    [menuButton setBackgroundImage:[UIImage imageNamed:@"menuButton.png"] forState:UIControlStateNormal];
    [self setHidden:NO];
    [self addSubview:menuButton];

    UIImageView *zblogo = [[UIImageView alloc] initWithImage:[self imageWithImage:[UIImage imageNamed:@"minilogo.png"] scaledToSize:CGSizeMake(200, 25)]];
    UINavigationItem *navI = [[UINavigationItem alloc] init];
    [self setItems:[NSArray arrayWithObject:navI]];

    [self.topItem setTitleView:zblogo];

    searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(0, 0, 20, 20);
    [searchButton setCenter:CGPointMake(self.bounds.size.width - 25, self.center.y)];
    [searchButton setBackgroundImage:[UIImage imageNamed:@"boutonRecherche.png"] forState:UIControlStateNormal];

    [self addSubview:searchButton];
}

@end

也许我在这里做错了什么?

更新

现在我的 NavigationController 已经很好地实例化了,但我仍然遇到问题:如果我写

UINavigationController *navController = [[UINavigationController alloc] initWithNavigationBarClass:[myNavBar class] toolbarClass:nil];
[navController setViewControllers:@[vc] animated:NO]

我在酒吧初始化中所做的一切都被删除了,并且

UINavigationController *navController = [[UINavigationController alloc] initWithNavigationBarClass:[myNavBar class] toolbarClass:nil];
[navController pushViewController:vc animated:NO]

我的导航栏出现一个“后退按钮”,我通过单击它来查看我的自定义。

我读过这个主题: Why does using setViewControllers remove everything from the UINavigationController's nav bar? 但它并没有真正帮助我。

我如何保留最初设置的导航栏?

stack overflow UINavigationController with custom NavigationBar
原文答案

答案:

作者头像

是否有可能您必须将 nil 放在第二个参数(工具栏参数)中才能使用默认参数?

从文档:

Specify the custom UIToolbar subclass you want to use, **or specify nil** to use the standard UIToolbar class.

更新,检查后我认为问题更多地朝这个方向发展,请检查这些链接:

How to add Button in UINavigationController

Adding BarButtons to a UINavigationBar after presenting a modal view controller

作者头像

如果你只是想把图像放在你的导航栏中,那么试试这个..在你的 AppDelegate didFinishLaunching 方法中

if ([self.navCtrl.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        UIImage *image = [UIImage imageNamed:@"YourImage.png"];
        [self.navCtrligationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }