我正在尝试使用自定义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]
我的navigationBar出现了一个“后退按钮”,我通过点击它看到我的自定义。
我已经阅读了这个主题:Why does using setViewControllers remove everything from the UINavigationController's nav bar?但它并没有真正帮助我。
我怎样才能保留我最初设置的navigationBar?
答案 0 :(得分:0)
是否有可能必须在第二个参数(工具栏一个)中加上 nil 才能使用默认值?
来自文档:
指定要使用的自定义UIToolbar子类,或指定nil 以使用标准UIToolbar类。
更新,检查后我认为问题更倾向于这个方向,请检查这些链接:
How to add Button in UINavigationController
Adding BarButtons to a UINavigationBar after presenting a modal view controller
答案 1 :(得分:-1)
如果您只想将图片放入导航栏,请在AppDelegate的didFinishLaunching方法中试试..
if ([self.navCtrl.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:@"YourImage.png"];
[self.navCtrligationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}