所以我想在点击按钮时更改文本和边框的颜色,而在未选择时将其更改回
这是我的代码
@objc func button_Select(_ sender: UIButton) {
let button = sender
if button.isSelected == true {
button.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
button.layer.cornerRadius = 10
button.layer.borderWidth = 1
button.layer.borderColor = #colorLiteral(red: 0.9424466491, green: 0.6981263757, blue: 0.6917206645, alpha: 1)
button.titleLabel?.textColor = #colorLiteral(red: 0.9424466491, green: 0.6981263757, blue: 0.6917206645, alpha: 1)
button.isSelected = false
} else {
button.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
button.layer.cornerRadius = 10
button.layer.borderWidth = 1
button.layer.borderColor = #colorLiteral(red: 0.6666666667, green: 0.6666666667, blue: 0.6666666667, alpha: 1)
button.titleLabel?.textColor=#colorLiteral(red: 0.6666666667, green: 0.6666666667, blue: 0.6666666667, alpha: 1)
button.isSelected = true
}
}
但是结果是,当选择按钮时,颜色不会变为粉红色;当未选择按钮时,按钮文本背景会变为灰色,而不是白色/清澈的颜色
答案 0 :(得分:0)
尝试为按钮分配标签,并将标签值更新为1或0,然后基于标签应用条件,就好像标签等于1,然后设置背景色
答案 1 :(得分:0)
Follow this below steps -
1.Choose UITextField from the object library
2.Drag to your storyboard
3.Choose border style as none.
4.Create a Swift file and add this below extension -
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}
extension UIButton {
func roundedButton(){
let maskPAth1 = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: [.topLeft , .topRight],
cornerRadii:CGSize(width:8.0, height:8.0))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = self.bounds
maskLayer1.path = maskPAth1.cgPath
self.layer.mask = maskLayer1
}
}
extension UITextField {
func setLeftPaddingPoints(_ amount:CGFloat){
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
self.leftView = paddingView
self.leftViewMode = .always
}
func setRightPaddingPoints(_ amount:CGFloat) {
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
self.rightView = paddingView
self.rightViewMode = .always
}
}
extension UITextField {
@IBInspectable var maxLength: Int {
get {
if let length = objc_getAssociatedObject(self, &kAssociationKeyMaxLength) as? Int {
return length
} else {
return Int.max
}
}
set {
objc_setAssociatedObject(self, &kAssociationKeyMaxLength, newValue, .OBJC_ASSOCIATION_RETAIN)
addTarget(self, action: #selector(checkMaxLength), for: .editingChanged)
}
}
@objc func checkMaxLength(textField: UITextField) {
guard let prospectiveText = self.text,
prospectiveText.count > maxLength
else {
return
}
let selection = selectedTextRange
let indexEndOfText = prospectiveText.index(prospectiveText.startIndex, offsetBy: maxLength)
let substring = prospectiveText[..<indexEndOfText]
text = String(substring)
selectedTextRange = selection
}
}
5.Now you can access this extensions either from storyboard or from code to change the values and see the effects.
6.You can change the corner radius, border width, border colour for UITextField.
Hope this method also helps
答案 2 :(得分:0)
使用bool值进行检查
// declare var
var checkBool = Bool()
// In viewDidLoad
checkBool = true
// On Button Action
@objc func button_Select(_ sender: UIButton) {
if checkBool == true{
button.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
button.layer.cornerRadius = 10
button.layer.borderWidth = 1
button.layer.borderColor = #colorLiteral(red: 0.9424466491, green: 0.6981263757, blue: 0.6917206645, alpha: 1)
button.setTitleColor(#colorLiteral(red: 0.9424466491, green: 0.6981263757, blue: 0.6917206645, alpha: 1), for: .normal)
// button.setTitleColor(.red, for: .normal)
checkBool = false
}else{
button.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
button.layer.cornerRadius = 10
button.layer.borderWidth = 1
button.layer.borderColor = #colorLiteral(red: 0.6666666667, green: 0.6666666667, blue: 0.6666666667, alpha: 1)
button.titleLabel?.textColor=#colorLiteral(red: 0.6666666667, green: 0.6666666667, blue: 0.6666666667, alpha: 1)
checkBool = true
}
}
希望它对您有用
答案 3 :(得分:0)
使用以下代码...可能会对您有帮助
@objc func button_Select(_ sender: UIButton) {
sender.tintColor = UIColor.clear
if sender.isSelected == true {
sender.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
sender.layer.cornerRadius = 10
sender.layer.borderWidth = 1
sender.layer.borderColor = #colorLiteral(red: 0.9424466491, green: 0.6981263757, blue: 0.6917206645, alpha: 1)
sender.setTitleColor(#colorLiteral(red: 0.9424466491, green: 0.6981263757, blue: 0.6917206645, alpha: 1), for: .normal)
sender.isSelected = false
} else {
sender.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
sender.layer.cornerRadius = 10
sender.layer.borderWidth = 1
sender.layer.borderColor = #colorLiteral(red: 0.6666666667, green: 0.6666666667, blue: 0.6666666667, alpha: 1)
sender.setTitleColor(#colorLiteral(red: 0.6666666667, green: 0.6666666667, blue: 0.6666666667, alpha: 1), for: .normal)
sender.isSelected = true
}
}