[英]setRightBarButtonItems doesn't work
I am trying to have 2 buttons on the right of a UINavigationBar
. 我想在
UINavigationBar
的右边有两个按钮。 Below is the source code. 以下是源代码。 No error but there is no button either.
没有错误,但也没有按钮。 It is a
UIViewController
, not a UINavigationViewController
它是一个
UIViewController
,而不是UINavigationViewController
let buttonEdit: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
buttonEdit.frame = CGRectMake(0, 0, 40, 40)
buttonEdit.setImage(UIImage(named:"me44.png"), forState: UIControlState.Normal)
buttonEdit.addTarget(self, action: "rightNavItemEditClick:", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItemEdit: UIBarButtonItem = UIBarButtonItem(customView: buttonEdit)
let buttonDelete: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
buttonDelete.frame = CGRectMake(0, 0, 40, 40)
buttonDelete.setImage(UIImage(named:"me44.png"), forState: UIControlState.Normal)
buttonDelete.addTarget(self, action: "rightNavItemDeleteClick:", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItemDelete: UIBarButtonItem = UIBarButtonItem(customView: buttonDelete)
// add multiple right bar button items
self.navigationController?.navigationItem.setRightBarButtonItems([rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)
//I also tried code below no luck either
self.navigationItem.setRightBarButtonItems([rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)
This code is just wrong: 这段代码错了:
self.navigationController?.navigationItem.setRightBarButtonItems(
[rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)
You do not set the navigation controller's navigationItem
; 您没有设置导航控制器的
navigationItem
; you set your navigationItem
. 你设置你的
navigationItem
。 Also, the [AnyObject]
thing is just unnecessary. 而且,
[AnyObject]
事情是不必要的。 So: 所以:
self.navigationItem.setRightBarButtonItems(
[rightBarButtonItemDelete, rightBarButtonItemEdit], animated: true)
However, note that that works only if your view controller is the child of a UINavigationController. 但请注意,仅当您的视图控制器是UINavigationController的子级时才有效。 Setting a view controller's
navigationItem
populates a navigation bar automatically only in that situation. 设置视图控制器的
navigationItem
仅在该情况下自动填充导航栏。 If you are not in that situation — ie, if you just have a "loose" navigation bar in your interface — you need to populate your navigation bar manually (by setting its navigationItem
). 如果您不处于这种情况 - 即,如果您的界面中只有一个“松散”导航栏 - 您需要手动填充导航栏(通过设置其
navigationItem
)。
(Also, note that if you have no "me44.png"
image, it could be that your code is working but you just aren't seeing anything.) (另请注意,如果您没有
"me44.png"
图像,则可能是您的代码正在运行,但您只是没有看到任何内容。)
Despite all indicated previously, it's possible that you don't see the added button to navigation bar. 尽管之前已经说明了,但您可能没有看到添加到导航栏的按钮。 For Swift 3, try this in the AppDelegate class (pay attention to topItem ):
对于Swift 3,请在AppDelegate类中尝试这个(注意topItem ):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.backgroundColor = UIColor.white
let navigationVC = UINavigationController(rootViewController: UIViewController())
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTodo))
navigationVC.navigationBar.topItem?.rightBarButtonItem = addButton
window?.rootViewController = navigationVC
window?.makeKeyAndVisible()
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.