Swift自定义UIView完全不响应点击

时间:2018-12-18 14:09:32

标签: ios swift

我正在创建一个简单的自定义UIView。然后将视图添加到视图控制器。一切都可见并就位,并且看起来应该看起来像。问题在于,没有任何响应可以响应点击。即文本字段永远不允许我输入,段控制不响应,按钮也不响应。我完全忽略了什么愚蠢的错误?

UIView:

class LoginView: UIView, UITextFieldDelegate {
    let orange = UIColor(red: 244/255.0, green: 123/255.0, blue: 32/255.0, alpha: 1)
    let green = UIColor(red: 0/255.0, green: 138/255.0, blue: 95/255.0, alpha: 1)
    let gray = UIColor(red: 128/255, green: 128/255, blue: 128/255, alpha: 1)

    let screenSize: ScreenSize = ScreenSize()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }

    private func setupView() {
        addSubview(cardView)

    }
var cardView: UIView {
        let cardView: UIView = UIView()
        cardView.frame = CGRect(x: 40, y: (screenSize.screenHeight / 2.5), width: (screenSize.screenWidth) - 80, height: 300);
        cardView.backgroundColor = UIColor.white
        cardView.layer.cornerRadius = 3.0
        cardView.layer.masksToBounds = false
        cardView.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
        cardView.layer.shadowOffset = CGSize(width: 0, height: 1)
        cardView.layer.shadowOpacity = 0.8
        cardView.addSubview(loginMethodSegmentControl)
        cardView.addSubview(emailIcon)
        cardView.addSubview(passwordIcon)
        cardView.addSubview(emailTextfield)
        cardView.addSubview(passwordTextField)
        cardView.addSubview(loginButton)
        cardView.addSubview(forgotPasswordButton)
        return cardView;
    }

var emailTextfield: UITextField {
        let emailTextfield: UITextField = UITextField()
        emailTextfield.frame = CGRect(x: 70, y: 100, width: (screenSize.screenWidth - 190), height: 30)
        emailTextfield.font = UIFont.systemFont(ofSize: 15)
        emailTextfield.autocorrectionType = UITextAutocorrectionType.no
        emailTextfield.spellCheckingType = UITextSpellCheckingType.no
        emailTextfield.placeholder = "Email Address"
        emailTextfield.isUserInteractionEnabled = true
        emailTextfield.isEnabled = true
        emailTextfield.keyboardType = .emailAddress
        emailTextfield.borderStyle = .none
        emailTextfield.layer.backgroundColor = UIColor.white.cgColor
        emailTextfield.layer.masksToBounds = false
        emailTextfield.layer.shadowColor = orange.cgColor
        emailTextfield.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        emailTextfield.layer.shadowOpacity = 1.0
        emailTextfield.layer.shadowRadius = 0.0
        emailTextfield.delegate = self
        return emailTextfield
    }

ViewController:

 override func viewDidLoad() {
        super.viewDidLoad()
loginView = LoginView(frame: CGRect.zero)
        self.view.addSubview(loginView)
}

2 个答案:

答案 0 :(得分:1)

帧为零,这是转发触摸的基础

loginView = LoginView(frame: CGRect.zero)

添加适当的框架或约束,例如

loginView = LoginView(frame: self.view.bounds)

答案 1 :(得分:0)

您的登录视图框架为零。由于容器视图未裁剪到边界,因此它正在显示视图。

 loginView = LoginView(frame: CGRect.zero)

您应该给rect

 loginView = LoginView(frame: self.view.bounds)