以编程方式添加的按钮在 ios 中不显示触摸感

Bal*_*yal 1 uibutton uiviewcontroller uiview ios swift

我已经在 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)
Run Code Online (Sandbox Code Playgroud)

CZ5*_*Z54 6

仅当按钮样式为 时才内置高亮效果System。您应该使用其他初始化程序,例如CustomButton( style : .system )