我有一个UIButton需要在用户滚动时根据UIScrollView中的哪个位置来更改背景颜色和文本。UIButton在滚动时丢失cornerRadius(当设置其他属性时)
当他们打了一个门槛,我调用下面的函数:
fileprivate func setFinalizeButton(format: FinalizeButtonFormat) {
switch format {
case ._continue:
let grey = UIColor(red: 189, green: 189, blue: 189)
finalizeButton?.setTitle("Continue", for: .normal)
finalizeButton?.setBackgroundColor(color: grey, forState: .normal)
finalizeButton?.setBackgroundColor(color: grey.darkerColor(percent: 0.2), forState: .highlighted)
case .buyNow:
finalizeButton?.setTitle("Buy Now", for: .normal)
finalizeButton?.setBackgroundColor(color: .black, forState: .normal)
finalizeButton?.setBackgroundColor(color: .black, forState: .highlighted)
}
}
我试过设置都finalizeButton?.cornerRadius
和finalizeButton?.layer.cornerRadius
也不工作。 cornerRadius的这两次都回到0.(该按钮初始化为cornerRadius的值为5)
任何想法为什么这是或如何解决它?
谢谢!
编辑:按钮设置
let finalizeButton = UIButton()
finalizeButton.cornerRadius = 5
finalizeButton.setTitleColor(.white, for: .normal)
finalizeButton.setTitleColor(UIColor.white.darkerColor(percent: 0.2), for: .highlighted)
finalizeButton.backgroundColor = .black
finalizeButton.addTarget(self, action: #selector(finalize(_:)), for: .touchUpInside)
finalizeButton.setTitle("Buy Now", for: .normal)
finalizeButton.titleLabel?.font = UIFont.systemFont(ofSize: 22, weight: UIFontWeightRegular)
bottomSection.addSubview(finalizeButton)
constrain(finalizeButton) {
$0.width == $0.superview!.width * CGFloat(0.8)
$0.height == CGFloat(54)
$0.bottom == $0.superview!.bottom - CGFloat(16)
$0.centerX == $0.superview!.centerX
}
self.finalizeButton = finalizeButton
其他地方的代码可能导致?请显示你正在做什么来操纵这个按钮,而不仅仅是你想象的是什么。 – matt
就是这样。我也会添加按钮的设置 –
@matt更新代码 –