我正在尝试创建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 的导航栏中删除所有内容?但它并没有真正帮助我。
我如何保留最初设置的导航栏?