我试图在我的文本字段中添加底部边框,但是它不起作用。我知道那里也有类似的问题,但这些答案都无济于事。另外,我在下面删除了这一行代码,并尝试执行此操作,因为默认情况下没有边框,但仍然无法使用:
username.borderStyle = UITextField.BorderStyle.none
这是整个代码:
class SignUpScreen: UIViewController {
let username = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(username)
// Background color
view.backgroundColor = .white
username.placeholder = "Name"
username.borderStyle = UITextField.BorderStyle.none
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0, y: view.frame.height - 1, width: view.frame.width, height: 1.0)
bottomLine.backgroundColor = UIColor.black.cgColor
username.layer.addSublayer(bottomLine)
username.translatesAutoresizingMaskIntoConstraints = false
username.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
username.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
username.widthAnchor.constraint(equalToConstant: 150).isActive = true
username.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
}
希望有人可以帮助我!谢谢! :)
答案 0 :(得分:4)
这是您缺少的内容:您正在使用主视图的框架创建图层的框架。您必须使用文本字段。
还应先在约束条件下正确绘制文本字段,然后在文本字段中添加图层。
在 viewDidLoad 中,您的内容视图已加载到主内存。该方法没有帧分配。因此,请使用 viewWillAppear 或 viewDidAppear 。
现在,当每次添加新的TextField和图层时将出现视图时。因此,您需要进行检查以限制该行为。例如如果文本字段已添加到您的视图中,则不要添加。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.addSubview(username)
/*
* Create the text field
*/
view.backgroundColor = .white
username.placeholder = "Name"
username.borderStyle = UITextField.BorderStyle.none
username.translatesAutoresizingMaskIntoConstraints = false
username.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
username.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
username.widthAnchor.constraint(equalToConstant: 300).isActive = true
username.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
/*
* Here add the shap layer
*/
let bottomLine = CALayer()
/*
* Here is what you were missing.
* You were using the main view's frame instead of your textfield
*/
bottomLine.frame = CGRect(x: 0, y: username.bounds.height - 1, width: username.bounds.width, height: 1.0)
bottomLine.backgroundColor = UIColor.black.cgColor
username.layer.addSublayer(bottomLine)
}
答案 1 :(得分:1)
bottomLine.frame = CGRect(x: 0, y: view.frame.height - 1, width: view.frame.width, height: 1.0)
您必须使用username.bounds
而不是view.frame
答案 2 :(得分:0)
除了添加图层而不是添加边界之外,还可以将其设置为UIView
并将其带到 top 并设置{ {1}} textField
作为bgColor
:
clearColor
答案 3 :(得分:0)
let border = CALayer()
let width = CGFloat(3.0)
border.borderColor = UIColor.black.cgColor
border.frame = CGRect(x: 0, y: textFieldOutlet.frame.size.height - width, width: textFieldOutlet.frame.size.width, height: textFieldOutlet.frame.size.height)
border.borderWidth = width
textFieldOutlet.layer.addSublayer(border)
textFieldOutlet.layer.masksToBounds = true
答案 4 :(得分:0)
尝试一下
func bottomBorderTextField(_ textField: UIView, color: UIColor) {
// For Bottom Border
let bottomLayer = CALayer()
let frame = viewType.frame
bottomLayer.frame = CGRect(x: 0, y: frame.height - 1, width: frame.width - 2, height: 0.5)
bottomLayer.backgroundColor = color.cgColor
viewType.layer.addSublayer(bottomLayer)
}
self.bottomBorderTextField(self.txtField, color: yourTextColor)