本文介绍了Swift 将多个图像添加到单个 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我想在一个 UIButton
上添加多个图像.
In my app, I would like to add multiple images on a single UIButton
.
这就是我现在所拥有的:
This is what I have now:
NextButton = UIButton(frame: CGRect(x: width*0, y: height*0.55, width: width*0.95, height: height*0.05))
NextButton.addTarget(self, action: #selector(OnNextPressed), for: .touchUpInside)
NextButton.isExclusiveTouch = true
NextButton.setTitle("ButtonText", for: UIControlState())
NextButton.setTitleColor(UIColor(red:0, green:0, blue:0, alpha:1.0), for: UIControlState())
NextButton.setBackgroundImage(UIImage(named: "ButtonOutline"), for: .normal)
view.addSubview(NextButton)
我想要第二张图片作为指向下一页的箭头.提前致谢:)
I would like a 2nd image as an arrow pointing to the next page.thanks in advance :)
按钮的其余部分有效,但我不知道如何使箭头出现
the rest of the button works but I don't know how to make the arrow appear
推荐答案
我在这里修复了新代码:
I fixed the problem here is the new code:
let screenSize: CGRect = UIScreen.main.bounds
let width = screenSize.width
let height = screenSize.height
NextButton = UIButton(frame: CGRect(x: width*0, y: height*0.55, width: width*0.95, height: height*0.05))
NextButton.addTarget(self, action: #selector(OnNextPressed), for: .touchUpInside)
NextButton.isExclusiveTouch = true
NextButton.setTitle("ButtonText", for: UIControlState())
NextButton.setTitleColor(UIColor(red:0, green:0, blue:0, alpha:1.0), for: UIControlState())
NextButton.setBackgroundImage(UIImage(named: "ButtonOutline"), for: .normal)
NextButton.setImage(UIImage(named: "arrow_next"), for: UIControlState())
NextButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, width*0.7)
view.addSubview(NextButton)
通过简单地将 setImage 添加到代码中,我可以在 BackgroundImage 之上创建第二个图像.然后通过起诉 imageEdgeInsets 我对齐按钮上的图像.
by simply adding setImage to the code i could create a second image on top of the BackgroundImage. then by suing imageEdgeInsets i aligned the image on the button.
这篇关于Swift 将多个图像添加到单个 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!