UIBarButtonItem在导航栏编程?
我一直在寻找这个解决scheme一段时间,但没有得到任何。 例如一个解决scheme是
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Stop, target: self, action: nil), animated: true)
这段代码将添加一个“停止”图像的button。 就像这样,还有其他的解决scheme,“search”,“刷新”等。但是,如果我想用我想要的图像编程添加button怎么办?
不设置button框的自定义button图像:
你可以使用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
检查这个苹果文件。 参考
UIBarButtonItem自定义button图像使用button框架
对于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)
对于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
对于设置多于一个的项目,使用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)
只需使用customView设置UIBarButtonItem
即可
例如:
var leftNavBarButton = UIBarButtonItem(customView:yourButton) self.navigationItem.leftBarButtonItem = leftNavBarButton
或者使用setFunction
:
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: yourButton), animated: true);
我只是偶然发现了这个问题,下面是Swift 3和iOS 10的更新:
let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "test.png"), style: .plain, target: self, action: nil) self.navigationItem.rightBarButtonItem = testUIBarButtonItem
这肯定比使用所有属性创buildUIButton快得多,然后将customView添加到UIBarButtonItem。
如果要将图像的颜色从默认蓝色更改为白色,则可以随时更改色调颜色:
test.tintColor = UIColor.white()
PS你应该明显地改变你的应用程序的select器等:)
在快捷键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") }
我有同样的问题,我已经阅读另一个话题的答案,然后我解决另一个类似的方式。 我不知道哪个更有效。 类似的问题
//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 ); }
用原始图像设置LeftBarButton。
let menuButton = UIBarButtonItem(image: UIImage(named: "imagename").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(classname.functionname)) self.navigationItem.leftBarButtonItem = menuButton