【发布时间】:2011-01-31 01:16:38
【问题描述】:
如何以编程方式向 UINavigationBar 添加按钮?
【问题讨论】:
标签: ios iphone xcode interface-builder uinavigationbar
如何以编程方式向 UINavigationBar 添加按钮?
【问题讨论】:
标签: ios iphone xcode interface-builder uinavigationbar
在NavigationBar 上设置rightbutton 的示例代码。
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[bar pushNavigationItem:item animated:NO];
但通常你会有一个NavigationController,让你可以写:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
【讨论】:
[rightbutton release](在最初撰写此评论时还没有)。
上面的答案很好,但我想用一些提示来充实它们:
如果您想修改后退按钮的标题(导航栏左侧的箭头-y),您必须在 PREVIOUS 视图控制器中进行,而不是在显示它的视图控制器中进行。这就像在说“嘿,如果你在这个视图控制器上推另一个视图控制器,请调用返回按钮“返回”(或其他)而不是默认按钮。”
如果您想在特殊状态下隐藏后退按钮,例如在显示 UIPickerView 时,请使用 self.navigationItem.hidesBackButton = YES; 并记住在您离开特殊状态时将其设置回来。
如果要显示特殊符号按钮之一,请使用 initWithBarButtonSystemItem:target:action 形式,其值类似于 UIBarButtonSystemItemAdd
请记住,该符号的含义取决于您,但请注意人机界面指南。使用 UIBarButtonSystemItemAdd 表示删除项目可能会导致您的应用程序被拒绝。
【讨论】:
将自定义按钮添加到导航栏(带有 buttonItem 的图像并指定操作方法 (void)openView{} 和)。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 32, 32);
[button setImage:[UIImage imageNamed:@"settings_b.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(openView) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
[barButton setCustomView:button];
self.navigationItem.rightBarButtonItem=barButton;
[button release];
[barButton release];
【讨论】:
下面的示例将在右侧的导航栏上显示一个标题为“联系人”的按钮。它的动作从视图控制器调用一个名为“contact”的方法。没有这条线,右键是不可见的。
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Contact"
style:UIBarButtonItemStylePlain target:self action:@selector(contact:)];;
【讨论】:
在 Swift 2 中,你会这样做:
let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = rightButton
(不是重大变化)在 Swift 4/5 中,它将是:
let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = rightButton
【讨论】:
为什么不使用以下内容: (来自Draw custom Back button on iPhone Navigation Bar)
// Add left
UINavigationItem *previousItem = [[UINavigationItem alloc] initWithTitle:@"Back title"];
UINavigationItem *currentItem = [[UINavigationItem alloc] initWithTitle:@"Main Title"];
[self.navigationController.navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil] animated:YES];
// set the delegate to self
[self.navigationController.navigationBar setDelegate:self];
【讨论】:
斯威夫特 3
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(cancelPressed(_:)))
cancelBarButton.setTitleTextAttributes( [NSFontAttributeName : UIFont.cancelBarButtonFont(),
NSForegroundColorAttributeName : UIColor.white], for: .normal)
self.navigationItem.leftBarButtonItem = cancelBarButton
func cancelPressed(_ sender: UIBarButtonItem ) {
self.dismiss(animated: true, completion: nil)
}
【讨论】: