简体   繁体   中英

Swift Custom UIView Nothing Responds to clicks

I'm creating a simple custom UIView. Then adding the view to the view controller. Everything is visible and in in place and looks like it should. The issue is that nothing is responding to clicks. IE textfields never allow me to type in, segment control doesn't respond, and the buttons don't respond. What dumb mistake am I completely overlooking?

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

The frame is zero which is the base for forwarding touches

loginView = LoginView(frame: CGRect.zero)

add a proper frame or constraints, for example

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

Your frame for login view is zero. It is displaying the view since the container view is not clip to bounds.

 loginView = LoginView(frame: CGRect.zero)

You should give frame rect

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM