How to add custom image in navigation bar button item?

前端 未结 12 1719
我寻月下人不归
我寻月下人不归 2020-12-13 09:26
 UIBarButtonItem *doneitem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)]autorelease];
           


        
12条回答
  •  生来不讨喜
    2020-12-13 10:06

    In case anyone needs Swift code for accepted answer:

    let infoImage = UIImage(named: "my-icon-32.png")
    let imgWidth = infoImage?.size.width
    let imgHeight = infoImage?.size.height
    let button:UIButton = UIButton(frame: CGRect(x: 0,y: 0,width: imgWidth!, height: imgHeight!))
    button.setBackgroundImage(infoImage, forState: .Normal)
    button.addTarget(self, action: Selector("openInfo"), forControlEvents: UIControlEvents.TouchUpInside)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
    

    P.S. Gurpreet Singh answer is working only for transparent PNG also I have to set button tintcolor other than clearColor.

提交回复
热议问题