IOS应用程序 - 不接收点击输入

时间:2016-08-31 12:40:07

标签: ios swift

我正在创建一款应用。到目前为止,我已经完成了一些基本的操作,删除了主要的故事板,并在主视图控制器中添加了一些组件,其中包含用于约束的代码和制图

基本上没有任何东西是可点击的,我有无法聚焦的UITextFields和无法点击的按钮。

为什么会这样?感谢

import UIKit
import Cartography

class LoginViewController: BaseViewController {

    var identifierField: UITextField!
    var passwordField: UITextField!

    override func loadUI() {
        view.clipsToBounds = true
        view.backgroundColor = UIColor.colorPrimary()

        let logoText = UILabel()

        let loginView = UIView()

        logoText.attributedText = NSMutableAttributedString.init(string: NSLocalizedString("APP_NAME", comment: ""))
        logoText.font = UIFont.boldSystemFontOfSize(32.0)

        self.view.addSubview(logoText)

        constrain(logoText, self.view) {
            logo, mainView in
            logo.top == mainView.top + 50
            logo.centerX == mainView.centerX
        }

        identifierField = UITextField()
        identifierField.borderStyle = .Line
        identifierField.placeholder = NSLocalizedString("USER_IDENTIFIER_PLACEHOLDER", comment: "")
        identifierField.textColor = UIColor.whiteColor()


        passwordField = UITextField()
        passwordField.borderStyle = .Line
        passwordField.placeholder = NSLocalizedString("PASSWORD_PLACEHOLDER", comment: "")
        passwordField.textColor = UIColor.whiteColor()


        let loginButton = UIButton()
        loginButton.setTitle(NSLocalizedString("LOGIN", comment: ""), forState: .Normal)

        let registrationButton = UIButton()
        registrationButton.setTitle(NSLocalizedString("REGISTER", comment: ""), forState: .Normal)
        registrationButton.addTarget(self, action: #selector(onRegistrationClicked(_:)), forControlEvents: .TouchUpInside)

        loginView.addSubview(identifierField)
        loginView.addSubview(passwordField)
        loginView.addSubview(loginButton)
        loginView.addSubview(registrationButton)

        view.addSubview(loginView)

        constrain(loginView, logoText, self.view) {
            loginView, logo, mainView in

            loginView.top == logo.bottom + 30
            loginView.width == mainView.width - 30
            loginView.centerX == mainView.centerX
        }

        constrain(loginView, identifierField, passwordField, loginButton, registrationButton) {
            loginView, identifierField, passwordField, loginButton, registrationButton in
            identifierField.top == loginView.top + 30
            identifierField.centerX == loginView.centerX
            identifierField.width == loginView.width

            passwordField.top == identifierField.bottom + 20
            passwordField.centerX == loginView.centerX
            passwordField.width == loginView.width

            loginButton.top == passwordField.bottom + 20
            loginButton.centerX == loginView.centerX
            loginButton.width == loginView.width

            registrationButton.top == loginButton.bottom + 20
            registrationButton.centerX == loginView.centerX
            registrationButton.width == loginView.width
        }
    }

    func onRegistrationClicked(sender: UIButton) {
        print("Register clicked")
        navigationController?.pushViewController(RegistrationViewController(), animated: true)
    }
}

1 个答案:

答案 0 :(得分:1)

使用userInteractionEnabled标志。 例如,在斯威夫特 -

identifierField.userInteractionEnabled = true