Swift将多个图像添加到单个UIButton中

时间:2016-10-11 13:47:09

标签: ios swift uibutton

enter image description here在我的应用中,我想在一个UIButton上添加多个图片。

这就是我现在所拥有的:

    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)

我希望第二张图片作为指向下一页的箭头。 提前谢谢:)

按钮的其余部分有效,但我不知道如何使箭头显示

1 个答案:

答案 0 :(得分:0)

我在这里修复了问题是新代码:

    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我对齐按钮上的图像。