0

I'm using material textfield in a view. But when this textfield's value is empty string the underline is not be shown. How can I fix it?

In the below image: email2 has empty string and the underline is not be shown.

enter image description here

Here is my view:

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

And Controller:

import UIKit
import Snapkit

class EditMailController: UIViewController {

    let editView = EditMailView(email1: "[email protected]", email2: "")

    override func viewDidLoad() {
        super.viewDidLoad()


        view.addSubview(editView)

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

        editView.email1.becomeFirstResponder()

    }
}
3
  • Where is your code ? Commented Dec 1, 2017 at 7:04
  • @iPatel will update question. Commented Dec 1, 2017 at 7:05
  • @iPatel I updated my question. Can you help me? Commented Dec 1, 2017 at 7:19

1 Answer 1

0

This issue has resolved. Instead try to set value for email2 with empty string like:

email1Input.text = e1
email2Input.text = e2

I ignored when e2 is empty string like this:

email1Input.text = e1
if !e2.isEmpty {
    email2Input.text = e2
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.