我已在IOS应用程序中的UIView中添加了UIButton。它与目标动作一起工作正常但是当我按下按钮时它没有显示屏幕中的按压感觉。 (正常按钮会在用户触摸时调暗其标题)。
我使用了以下代码:
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setupViewLayouts()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupViewLayouts()
}
func setupViewLayouts() {
titleLabel?.font = UIFont.boldSystemFont(ofSize: 16.0)
backgroundColor = .black
layer.borderWidth = 1.0
layer.cornerRadius = 3.0
layer.borderColor = UIColor.clear.cgColor
setTitleColor(UIColor.black, for: .normal)
}
}
var btnLogin: CustomButton = {
let button = CustomButton()
button.setTitle("Login", for: .normal)
button.addTarget(self, action: #selector(btnLogin_Tapped), for: .touchUpInside)
return button
}()
addSubview(btnSignIn)
答案 0 :(得分:3)
仅当按钮样式为System
时,才会内置高亮效果。您应该使用其他初始值设定项,例如CustomButton( style : .system )
答案 1 :(得分:2)
使用以下扩展名:
extension UIButton {
func setBackgroundColor(color: UIColor, for state: UIControlState) {
let image = UIImage.image(with: color)
let insets = UIEdgeInsetsMake(0, 0, 0, 0)
let stretchable = image.resizableImage(withCapInsets: insets, resizingMode: .tile)
self.setBackgroundImage(stretchable, for: state)
}
}
而不是这个:
backgroundColor = .black
使用它:
setBackgroundColor(color: .black, for: .normal)
直接设置背景颜色会使突出显示变得混乱。