How to set image for bar button with swift?

前端 未结 14 1215
一个人的身影
一个人的身影 2020-11-28 04:22

I am trying to set an Image for bar button Item for that I have an image like:

\"enter

14条回答
  •  一个人的身影
    2020-11-28 05:28

    Similar to the accepted solution, but you can replace the

    let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    

    with

    let button = UIButton()
    

    Here is the full solution, enjoy: (it's just a bit cleaner than the accepted solution)

    let button = UIButton()
    button.frame = CGRectMake(0, 0, 51, 31) //won't work if you don't set frame
    button.setImage(UIImage(named: "fb"), forState: .Normal)
    button.addTarget(self, action: Selector("fbButtonPressed"), forControlEvents: .TouchUpInside)
    
    let barButton = UIBarButtonItem()
    barButton.customView = button
    self.navigationItem.rightBarButtonItem = barButton
    

提交回复
热议问题