I am trying set the background image of UIBarButtonItem programatically. But my image is stretched and tiled. Any help on fixing this would be much appreciated. Here is my code
var objectSelected : UIImage!{
didSet{
if objectSelected == nil {
objectSelected = UIImage(named: "line")
}
let insets = UIEdgeInsetsMake(0, 0, 10, 0)
objectSelected.resizableImageWithCapInsets(insets)
objectSelector.setBackgroundImage(objectSelected, forState: UIControlState.Normal, style: UIBarButtonItemStyle.Plain, barMetrics: UIBarMetrics.Default)
}
}
@IBOutlet weak var objectSelector: UIBarButtonItem!
You could try this.
var myBtn: UIButton = UIButton()
myBtn.setImage(UIImage(named: "line"), forState: .Normal)
myBtn.frame = CGRectMake(0, 0, 70, 70)
myBtn.addTarget(self, action: "myBtnAction:", forControlEvents: .TouchUpInside)
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: myBtn), animated: true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With