当值为空时,TextField不显示底线字符串Material Swift

时间:2017-12-01 07:03:12

标签: ios swift cosmicmind

我在视图中使用material textfield。但是当此textfield's值为空时,不会显示下划线。我该如何解决?

在下图中:email2为空string,并且未显示下划线。

enter image description here

以下是我的观点:

import UIKit
import SnapKit
import Material

class EditMailView: UIView {

    public let email1: UILabel = {
        let lb = UILabel()
        lb.text = "email1"
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    }()

    public let email2: UILabel = {
        let lb = UILabel()
        lb.text = "email2"
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    }()

    public let email1Input: TextField = {
        let field = TextField()
        field.font = UIFont.systemFont(ofSize: 15)
        return field
    }()

    public let email2Input: TextField = {
        let field = TextField()
        field.font = UIFont.systemFont(ofSize: 15)
        return field
    }()


    public let email1Counter: UILabel = {
        let lb = UILabel()
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    }()

    public let email2Counter: UILabel = {
        let lb = UILabel()
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    }()


    convenience init(email1 e1: String, email2 e2: String) {
        self.init(frame: .zero)

        email1Input.text = e1
        email2Input.text = e2

        addSubview(email1)
        addSubview(email2)
        addSubview(email1Input)
        addSubview(email2Input)
        addSubview(email1Counter)
        addSubview(email2Counter)

        email1.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email1Input.snp.top).offset(-10)
        }

        email1Input.snp.makeConstraints { make in
            make.top.equalTo(email1.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email1Counter.snp.top).offset(-10)
        }

        email1Counter.snp.makeConstraints { make in
            make.top.equalTo(email1Input.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email2.snp.top).offset(-10)
        }

        email2.snp.makeConstraints { make in
            make.top.equalTo(email1Counter.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email2Input.snp.top).offset(-10)
        }

        email2Input.snp.makeConstraints { make in
            make.top.equalTo(email2.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalTo(email2Counter.snp.top).offset(-10)
        }

        email2Counter.snp.makeConstraints { make in
            make.top.equalTo(email2Input.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(10)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
}

和控制器:

import UIKit
import Snapkit

class EditMailController: UIViewController {

    let editView = EditMailView(email1: "123@abc.xyz", email2: "")

    override func viewDidLoad() {
        super.viewDidLoad()


        view.addSubview(editView)

        editView.snp.makeConstraints { (maker) in
            maker.edges.equalToSuperview()
        }

        editView.email1.becomeFirstResponder()

    }
}

1 个答案:

答案 0 :(得分:0)

此问题已解决。而是尝试使用空字符串为email2设置值,如:

email1Input.text = e1
email2Input.text = e2

当e2是这样的空字符串时我忽略了:

email1Input.text = e1
if !e2.isEmpty {
    email2Input.text = e2
}