gpt4 book ai didi

ios - setRightBarButtonItems 不起作用

转载 作者:可可西里 更新时间:2023-11-01 00:16:22 24 4
gpt4 key购买 nike

我想在 UINavigationBar 的右侧设置 2 个按钮。下面是源代码。没有错误,但也没有按钮。它是一个 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)

最佳答案

这段代码是错误的:

self.navigationController?.navigationItem.setRightBarButtonItems(
[rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)

您没有设置导航 Controller 的navigationItem;您设置您的 navigationItem。此外,[AnyObject] 是不必要的。所以:

self.navigationItem.setRightBarButtonItems(
[rightBarButtonItemDelete, rightBarButtonItemEdit], animated: true)

但是,请注意,只有当您的 View Controller 是 UINavigationController 的子级时,它才有效。设置 View Controller 的 navigationItem 只会在那种情况下自动填充导航栏。如果您处于那种情况——即,如果您的界面中只有一个“松散”的导航栏——您需要手动填充导航栏(通过设置 < em>它的 navigationItem).

(另外,请注意,如果您没有"me44.png" 图像,则可能是您的代码正在运行,但您只是看不到任何东西。 )

关于ios - setRightBarButtonItems 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31142890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com