UiTextField leftViewMode奇怪的行为.. Bug?

时间:2016-04-20 00:52:54

标签: ios swift

我有两个textFields,每个textField我想在leftImageView中添加一个图标。当我尝试加载此视图时,UI会挂起并且永远不会加载。如果我注释掉其中一个标记块。我的UI加载很好,我最终得到了一个带有iconView图标的文本字段。

我的问题是在两个textFields中都有一个图标。

    for textField in textFields {
        textField.delegate = self
        textField.autocorrectionType = .No
        textField.leftViewMode = .Always

        if textField.tag == 1 {
            textField.keyboardType = .EmailAddress
            textField.attributedPlaceholder = attributedText("Username")

            leftImageViews[0].image = UIImage(named: "user")
            leftImageViews[0].contentMode = .ScaleAspectFit
            leftTextFieldViews[0].addSubview(leftImageViews[0])
            textField.leftView = leftTextFieldViews[0]
            textField.leftView?.frame = leftTextFieldViews[0].frame

        } else if textField.tag == 2 {
            textField.secureTextEntry = true
            textField.attributedPlaceholder = attributedText("Password")

            leftImageViews[1].image = UIImage(named: "password")
            leftImageViews[1].contentMode = .ScaleAspectFit
            leftTextFieldViews[1].addSubview(leftImageViews[1])
            textField.leftView = leftTextFieldViews[1]
            textField.leftView?.frame = leftTextFieldViews[1].frame

        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个

 @IBOutlet var collectionOftextfields: Array<UITextField>?

 for i in 0 ..< collectionOftextfields!.count {
        let textfieldimage = UIImageView()
        let image = UIImage(named: "xyz.png")
        textfieldimage.image = image
        textfieldimage.frame = CGRect(x: 0,y: 200,width: 8,height: 8)
        var myField = UITextField()
        myField = collectionOftextfields![i]
        myField.leftView = textfieldimage
        myField.leftViewMode = UITextFieldViewMode.Always
    }

如果您需要有关如何将文本字段添加到集合中的信息,请告诉我们 希望这会有所帮助。