我正在尝试以编程方式设置UIBarButtonItem的背景图像.但我的图像被拉伸和平铺.任何帮助解决这个问题将非常感激.这是我的代码 var objectSelected : UIImage!{ didSet{ if objectSelected == nil { objectSe
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!你可以试试这个.
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)