gpt4 book ai didi

swift - 在 swift 中为 UIBarButtonItem 设置背景图像

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:01 25 4
gpt4 key购买 nike

我正在尝试以编程方式设置 UIBarButtonItem 的背景图像。但是我的图像被拉伸(stretch)和平铺。解决此问题的任何帮助将不胜感激。这是我的代码

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)

关于swift - 在 swift 中为 UIBarButtonItem 设置背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com