我一直在寻找这个解决方案一段时间,但是没有得到任何解决方案。 例如,一种解决方案是
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Stop, target: self, action: nil), animated: true)
此代码将添加一个按钮,其中包含" stop"图片。就像这样,还有其他解决方案"搜索,"刷新"但如果我想以编程方式添加一个按钮,我想要的图像是什么?
答案 0 :(得分:303)
不设置按钮框的自定义按钮图像:
您可以使用init(image: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?)
使用指定的图像和其他属性初始化新项目。
let button1 = UIBarButtonItem(image: UIImage(named: "imagename"), style: .plain, target: self, action: Selector("action")) // action:#selector(Class.MethodName) for swift 3
self.navigationItem.rightBarButtonItem = button1
FOR Swift 3.0
let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "imagename"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn1.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)
let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "imagename"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn2.addTarget(self, action: #selector(Class.MethodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)
self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)
FOR Swift 2.0
及更早
let btnName = UIButton()
btnName.setImage(UIImage(named: "imagename"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)
//.... Set Right/Left Bar Button item
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = btnName
self.navigationItem.rightBarButtonItem = rightBarButton
或者只需使用init(customView:)之类的
let rightBarButton = UIBarButtonItem(customView: btnName) self.navigationItem.rightBarButtonItem = rightBarButton
对于系统UIBarButtonItem
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
self.navigationItem.rightBarButtonItem = camera
设置超过1个项目时使用rightBarButtonItems
或左侧leftBarButtonItems
let btn1 = UIButton()
btn1.setImage(UIImage(named: "img1"), forState: .Normal)
btn1.frame = CGRectMake(0, 0, 30, 30)
btn1.addTarget(self, action: Selector("action1:"), forControlEvents: .TouchUpInside)
let item1 = UIBarButtonItem()
item1.customView = btn1
let btn2 = UIButton()
btn2.setImage(UIImage(named: "img2"), forState: .Normal)
btn2.frame = CGRectMake(0, 0, 30, 30)
btn2.addTarget(self, action: Selector("action2:"), forControlEvents: .TouchUpInside)
let item2 = UIBarButtonItem()
item2.customView = btn2
self.navigationItem.rightBarButtonItems = [item1,item2]
使用setLeftBarButtonItem
或setRightBarButtonItem
let btn1 = UIButton()
btn1.setImage(UIImage(named: "img1"), forState: .Normal)
btn1.frame = CGRectMake(0, 0, 30, 30)
btn1.addTarget(self, action: Selector("action1:"), forControlEvents: .TouchUpInside)
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: btn1), animated: true);
对于swift> = 2.2,操作应为
#selector(Class.MethodName)
...例如btnName.addTarget(self, action: #selector(Class.MethodName), forControlEvents: .TouchUpInside)
答案 1 :(得分:19)
使用Swift 4
或Swift 4.2
在ViewDidLoad
方法内,定义您的按钮并将其添加到导航栏中。
override func viewDidLoad() {
super.viewDidLoad()
let logoutBarButtonItem = UIBarButtonItem(title: "Logout", style: .done, target: self, action: #selector(logoutUser))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
}
然后您需要定义在action参数内提到的功能,如下所示
@objc func logoutUser(){
print("clicked")
}
您需要添加@objc
前缀,因为它仍在使用旧版东西(目标C)。
答案 2 :(得分:17)
只需使用customView
设置UIBarButtonItem
即可
例如:
var leftNavBarButton = UIBarButtonItem(customView:yourButton)
self.navigationItem.leftBarButtonItem = leftNavBarButton
或使用setFunction
:
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: yourButton), animated: true);
答案 3 :(得分:11)
我只是偶然发现了这个问题,这里是Swift 3和iOS 10的更新:
let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "test.png"), style: .plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem = testUIBarButtonItem
它肯定比使用所有属性创建UIButton然后随后将customView添加到UIBarButtonItem快得多。
如果您想将图像的颜色从默认的蓝色更改为例如白色,您可以随时更改色调颜色:
test.tintColor = UIColor.white()
PS您显然应该更改应用程序的选择器等:)
答案 4 :(得分:5)
在快捷方式3中,在按钮中执行操作:
let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "test.png"), style: .plain, target: self, action: #selector(ViewController.clickButton))
self.navigationItem.rightBarButtonItem = testUIBarButtonItem
func clickButton(){
print("button click")
}
答案 5 :(得分:2)
我有同样的问题,我已经阅读了另一个主题的答案,然后我解决了另一个类似的方式。我不知道哪个更有效。 similar issue
//play button
@IBAction func startIt(sender: AnyObject) {
startThrough();
};
//play button
func startThrough() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true);
let pauseButton = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "pauseIt");
self.toolBarIt.items?.removeLast();
self.toolBarIt.items?.append( pauseButton );
}
func pauseIt() {
timer.invalidate();
let play = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "startThrough");
self.toolBarIt.items?.removeLast();
self.toolBarIt.items?.append( play );
}
答案 6 :(得分:2)
使用原始图像设置LeftBarButton。
let menuButton = UIBarButtonItem(image: UIImage(named: "imagename").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(classname.functionname))
self.navigationItem.leftBarButtonItem = menuButton
答案 7 :(得分:1)
适用于 Swift 5+
let searchBarButtonItem = UIBarButtonItem(image: UIImage(named: "searchIcon"), style: .plain, target: self, action: #selector(onSearchButtonClicked))
self.navigationItem.rightBarButtonItem = searchBarButtonItem
@objc func onSearchButtonClicked(_ sender: Any){
print("SearchButtonClicked")
}
答案 8 :(得分:0)
func viewDidLoad(){
let homeBtn: UIButton = UIButton(type: UIButtonType.custom)
homeBtn.setImage(UIImage(named: "Home.png"), for: [])
homeBtn.addTarget(self, action: #selector(homeAction), for: UIControlEvents.touchUpInside)
homeBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let homeButton = UIBarButtonItem(customView: homeBtn)
let backBtn: UIButton = UIButton(type: UIButtonType.custom)
backBtn.setImage(UIImage(named: "back.png"), for: [])
backBtn.addTarget(self, action: #selector(backAction), for: UIControlEvents.touchUpInside)
backBtn.frame = CGRect(x: -10, y: 0, width: 30, height: 30)
let backButton = UIBarButtonItem(customView: backBtn)
self.navigationItem.setLeftBarButtonItems([backButton,homeButton], animated: true)
}
}
答案 9 :(得分:0)
这是苹果的疯子。当您说出self.navigationItem.rightBarButtonItem.title时,它将显示为nil,而在GUI上则显示Edit或Save。像我这样的新生会花很多时间来调试这种行为。
有一个要求,即该项目将在首次加载时显示“编辑”,然后用户点击它,它将变为“保存标题”。要对此进行存档,我做了如下操作。
//加载的视图会显示“编辑标题”
private func loadRightBarItem() {
let logoutBarButtonItem = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(handleEditBtn))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
}
//点击“编辑项目”将更改为“保存标题”
@objc private func handleEditBtn() {
print("clicked on Edit btn")
let logoutBarButtonItem = UIBarButtonItem(title: "Save", style: .done, target: self, action: #selector(handleSaveBtn))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
blockEditTable(isBlock: false)
}
//点击“保存”项目将显示“编辑标题”
@objc private func handleSaveBtn(){
print("clicked on Save btn")
let logoutBarButtonItem = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(handleEditBtn))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
saveInvitation()
blockEditTable(isBlock: true)
}
答案 10 :(得分:0)
iOS 11
使用约束设置自定义按钮:
let buttonWidth = CGFloat(30)
let buttonHeight = CGFloat(30)
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "img name"), for: .normal)
button.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
button.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true
button.heightAnchor.constraint(equalToConstant: buttonHeight).isActive = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: button)
答案 11 :(得分:0)
除上述之外,您还可以在 ios14 及以上版本中使用以下内容
if #available(iOS 14.0, *) {
let closeAction = UIAction(handler: { [weak self] _ in
//perform action here
})
let closeBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: closeAction, menu: nil)
navigationItem.rightBarButtonItem = closeBarButtonItem
}