如何在Swift 3中突出显示(或点击)按钮时更改UIButton边框颜色?

时间:2017-01-25 03:36:48

标签: ios mobile uibutton swift3

这就是我现在所拥有的。想知道如何在点击或突出显示按钮时更改按钮的边框颜色。到目前为止,我只能使用按钮的文本颜色来完成此操作。任何帮助或提示都会很棒,谢谢。

@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
    }

}

3 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是为touchUp和touchDown操作附加目标,如下所示:

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
}

答案 1 :(得分:0)

您可以为高光状态指定背景图像(边框样式)。 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);

答案 2 :(得分:0)

如果您只想在触摸是的同时更改颜色,而在触摸是 UP 的情况下还原颜色,则只需使用isHighlighted作为子类的 UIButton ,我认为这是一种更清洁的方法。像这样:

第一种情况:

enter image description here

     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
                }
            }
        }
    }

现在,如果您有很多按钮,并且希望用户知道所选的按钮,您可以尝试以下代码,尽管我知道还有很多其他方法可以实现此目的,但这是我想出的最快方法。 代码说明了自己。

第二种情况:

enter image description here

    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)
            }

        }
    }