Ios 当按钮在Swift 3中高亮显示(或点击)时,如何更改UIButton边框颜色?
这就是我现在拥有的。想知道在点击或突出显示按钮时如何更改按钮的边框颜色。到目前为止,我只能用按钮的文本颜色来实现这一点。任何帮助或提示都会很好,谢谢Ios 当按钮在Swift 3中高亮显示(或点击)时,如何更改UIButton边框颜色?,ios,mobile,uibutton,swift3,Ios,Mobile,Uibutton,Swift3,这就是我现在拥有的。想知道在点击或突出显示按钮时如何更改按钮的边框颜色。到目前为止,我只能用按钮的文本颜色来实现这一点。任何帮助或提示都会很好,谢谢 @IBOutlet weak var createNewWorkoutButton: UIButton! @IBAction func createWorkoutBtn(_ sender: UIButton) { } override func viewDidLoad() { super.viewDidLoad() crea
@IBOutlet weak var createNewWorkoutButton: UIButton!
@IBAction func createWorkoutBtn(_ sender: UIButton) {
}
override func viewDidLoad() {
super.viewDidLoad()
createNewWorkoutButton.setTitleColor(UIColor .gray, for: UIControlState.highlighted)
if createNewWorkoutButton = UIControlState.highlighted {
createNewWorkoutButton.layer.borderColor = UIColor.gray.cgColor
}
}
实现这一点的一种方法是为接地和着地动作附加目标,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
createNewWorkoutButton.setTitleColor(UIColor .gray, for: UIControlState.highlighted)
createNewWorkoutButton.addTarget(self, action: #selector(setButtonSelected(button:)), for: .touchDown);
createNewWorkoutButton.addTarget(self, action: #selector(setButtonSelected(button:)), for: .touchUpInside);
}
func setButtonSelected(button : UIButton) {
//layout for selected state
}
func setButtonUnselected(button : UIButton) {
//layout for unselected state
}
可以为高亮度状态指定背景图像(边框样式)。e、 g:
UIGraphicsBeginImageContext(createNewWorkoutButton.frame.size);
let context = UIGraphicsGetCurrentContext();
context?.setStrokeColor(UIColor.red.cgColor);
context?.setLineWidth(1.0);
context?.stroke(CGRect.init(x: 0, y: 0, width: createNewWorkoutButton.frame.size.width,
height: createNewWorkoutButton.frame.size.height));
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
createNewWorkoutButton.setBackgroundImage(image, for: UIControlState.highlighted);
如果您只想在触摸键向下时更改颜色,并在触摸键向上时将其还原,只需通过子类化UIButton使用
iHighlighted
,我认为这是一种更干净的方法。像这样:
第一个场景:
现在,如果您有许多按钮,并且希望用户知道所选的按钮,您可以尝试下面的代码,尽管我知道有很多其他方法可以实现这一点,但这是我想到的最快的方法。
代码解释了它自己
第二种情况:
谢谢你的输入!谢谢你的意见!
class MyButton: UIButton
{
override var isHighlighted: Bool {
didSet {
switch isHighlighted {
case true:
layer.borderColor = UIColor.blue.cgColor
layer.borderWidth = 2
case false:
layer.borderColor = UIColor.lightGray.cgColor
layer.borderWidth = 1
}
}
}
}
class Something: UIView {
private lazy var firstButton: UIButton = {
let btn = UIButton(type: .system)
btn.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
return btn
}()
private lazy var secondButton: UIButton = {
let btn = UIButton(type: .system)
btn.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
return btn
}()
private lazy var thirdButton: UIButton = {
let btn = UIButton(type: .system)
btn.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
return btn
}()
@objc
private func buttonTapped(_ sender: UIButton) {
let myButtons = [self.firstButton, self.secondButton, self.thirdButton]
for button in vatButtons {
// state 1 means the button is on Selected State
(button.state.rawValue == 1) ? button.layer.borderColor = UIColor.blue.cgColor : (button.layer.borderColor = UIColor.lightGray.withAlphaComponent(0.5).cgColor)
}
}
}