ios - 快速将多个图像添加到单个 UIButton

标签 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)

我想要第二张图像作为指向下一页的箭头。 预先感谢:)

按钮的其余部分可以工作,但我不知道如何使箭头出现

最佳答案

我解决了这里的问题是新代码:

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

关于ios - 快速将多个图像添加到单个 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39978780/

相关文章:

ios - 带有 Swift 的 Google Objective-C API 'GTL'

ios - 使用NSUserDefaults的同步操作GCD队列导致的死锁

ios - Xcode 更新不支持 iPhone 5s

swift - 在 SwiftUI 中强制 View 位于 View 的右下角

ios - 单击时更新标签

iphone - Objective-C setImage forState :UIControlStateNormal not formatting image properly

ios - 通过比较字符串从Xcode中的NSArray中选择indexOfObject

ios - 升级Xcode后项目有bug

ios - UIButton 仅在 UIControlStateNormal 上设置图像

ios - 多个持久化 View Controller