2

我正在尝试创建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]

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

我已阅读此主题:为什么使用 setViewControllers 会从 UINavigationController 的导航栏中删除所有内容?但它并没有真正帮助我。

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

4

2 回答 2

0

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

从文档:

指定要使用的自定义 UIToolbar 子类,或指定 nil以使用标准 UIToolbar 类。

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

如何在 UINavigationController 中添加按钮

在呈现模态视图控制器后将 BarButtons 添加到 UINavigationBar

于 2013-06-05T12:39:53.870 回答
-1

如果您只想将图像放在导航栏中,请在 AppDelegate 的 didFinishLaunching 方法中尝试这个 ..

if ([self.navCtrl.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        UIImage *image = [UIImage imageNamed:@"YourImage.png"];
        [self.navCtrligationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }
于 2013-06-05T12:46:22.743 回答