You can also use auto-layout as an alternative
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
let bottomLine: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .red
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
textField.borderStyle = .none
bottomLineViewSetup()
}
func bottomLineViewSetup() {
view.addSubview(bottomLine)
NSLayoutConstraint.activate([
bottomLine.leadingAnchor.constraint(equalTo: textField.leadingAnchor),
bottomLine.trailingAnchor.constraint(equalTo: textField.trailingAnchor),
bottomLine.topAnchor.constraint(equalTo: textField.bottomAnchor),
bottomLine.heightAnchor.constraint(equalToConstant: 2.0)
])
}
}
To learn more about auto-layout you can check this link
and if you want to go further about building an app programmatically you can also check this article