2016-09-18 43 views
0

我尝试以编程方式添加UINavigationBar并设置栏按钮项目。 我想:没有出现右栏按钮

self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 
    self.artificialNavBar.backgroundColor = [UIColor whiteColor]; 
    UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:@"Готово" style:UIBarButtonItemStyleDone target:nil action:nil]; 
    UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil]; 
UINavigationItem *navItem = [[UINavigationItem alloc] init]; 

    navItem.leftBarButtonItem = bbiDone; 
    navItem.rightBarButtonItem = bbiTry; 

    self.artificialNavBar.items = @[ navItem ]; 
    [self.view addSubview:self.artificialNavBar]; 

然而,似乎只剩栏按钮,右侧是隐藏的。我错过了什么?

+0

显示'artificialNavBar'声明。 –

+0

@ Mr.UB属性(非原子)UINavigationBar * artificialNavBar; –

+0

也使这强劲。 –

回答

1

在你写这些,然后用下面的代码替换代码这个类的.h文件中声明

@property (nonatomic, strong) UINavigationItem *navItem; 

self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 
self.artificialNavBar.backgroundColor = [UIColor whiteColor]; 
[self.view addSubview:self.artificialNavBar]; 

UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:@"Готово" style:UIBarButtonItemStyleDone target:nil action:nil]; 
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil]; 

self.navItem = [[UINavigationItem alloc] init]; 
[self.navItem setLeftBarButtonItem:bbiDone animated:NO] 
[self.navItem setRightBarButtonItem:bbiTry animated:NO] 

[self.artificialNavBar setItems:@[self.navItem] animated:NO] 

并确保navbar是非原子和强大的。

+0

即时通讯不使用故事板.. –

+0

没问题。你可以使用这段代码。只是删除iboutlet ..这是一个错误。我已经更新了答案 –

+0

好吧,我尝试,顺便说一句,属性是默认强,如果你不指定其他。 –