Ios 文本字段的下边框未显示
我试图在我的文本字段中添加一个底部边框,但它不起作用。我知道也有类似的问题,但这些答案都没有帮助。另外,我删除了下面这行代码并尝试了,因为默认情况下没有边框,它仍然不起作用:Ios 文本字段的下边框未显示,ios,swift,border,Ios,Swift,Border,我试图在我的文本字段中添加一个底部边框,但它不起作用。我知道也有类似的问题,但这些答案都没有帮助。另外,我删除了下面这行代码并尝试了,因为默认情况下没有边框,它仍然不起作用: username.borderStyle = UITextField.BorderStyle.none 以下是全部代码: class SignUpScreen: UIViewController { let username = UITextField() override func viewDidL
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
}
}
希望有人能帮助我!谢谢 bottomLine.frame=CGRectx:0,y:view.frame.height-1,width:view.frame.width,height:1.0
您必须使用username.bounds,而不是view.framebottomLine.frame=CGRectx:0,y:view.frame.height-1,width:view.frame.width,height:1.0
您必须使用username.bounds,而不是view.frame,而不是添加图层。也许您应该将边框添加为UIView,并将其置于顶部。还可以将文本字段bgColor设置为clearColor:
与其添加图层,不如将边框添加为UIView并置于顶部,还可以将文本字段bgColor设置为clearColor:
这里是您缺少的:您使用主视图的框架来创建层的框架。必须使用文本字段
另外,首先使用约束正确绘制文本字段,然后将图层添加到文本字段
在viewDidLoad中,内容视图将加载到主内存。该方法中没有帧指定。因此,请使用ViewWillDisplay或ViewDidDisplay
现在,每次添加新的文本字段和图层时,视图都会出现。所以你需要做一个检查来限制这种行为。例如,如果文本字段已添加到视图中,则不要添加
这里是您缺少的:您使用主视图的框架来创建层的框架。必须使用文本字段
另外,首先使用约束正确绘制文本字段,然后将图层添加到文本字段
在viewDidLoad中,内容视图将加载到主内存。该方法中没有帧指定。因此,请使用ViewWillDisplay或ViewDidDisplay
现在,每次添加新的文本字段和图层时,视图都会出现。所以你需要做一个检查来限制这种行为。例如,如果文本字段已添加到视图中,则不要添加
试试这个
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)
}
在viewDidload中
试试这个
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)
}
在viewDidload中
将层的帧更新放在viewDidLayoutSubviews中。很抱歉,我没有理解您的意思,介意解释一下吗?在viewDidLoad期间,username具有初始帧。然后它将更新其框架以匹配自动布局约束。所以,您应该在布局完成后手动更新子层框架。或者,尝试在ViewDidDisplay中运行代码,看看它是否有帮助是的,它是正确的,但是在将层添加到某个视图之前,我们必须首先布局该视图。意味着在约束之后,我们可以添加层@AbdullahAjmal请查看我的答案,将图层的框架更新为viewDidLayoutSubviews。很抱歉,我没有理解您的意思,介意解释一下吗?在viewDidLoad期间,用户名具有初始框架。然后它将更新其框架以匹配自动布局约束。所以,您应该在布局完成后手动更新子层框架。或者,尝试在ViewDidDisplay中运行代码,看看它是否有帮助是的,它是正确的,但是在将层添加到某个视图之前,我们必须首先布局该视图。意味着在约束之后,我们可以添加层@AbdullahAjmal请看我的答案嘿,我是swift的新手,我不知道你在这里做了什么,我能知道你是从哪里学到这些的吗?您是否可以提供网站或视频链接?谢谢顺便问一下,我可以将此添加到另一个文本字段吗?我有一个名为password的文本字段,添加了以下内容:password.layer.addSublayerbottomLine。这行吗?不,你需要用密码字段的框架创建一个新层。如果您使用相同的,它将添加到您的用户名字段。嘿,我是swift新手,我不知道您在这里做了什么,我能知道您从哪里学到这些吗?您是否可以提供网站或视频链接?谢谢顺便问一下,我可以将此添加到另一个文本字段吗?我有一个名为password的文本字段,添加了以下内容:password.layer.addSublayerbottomLine。这行吗?不,你需要用密码字段的框架创建一个新层。如果您将使用相同的,它将添加到您的用户名字段中。
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
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)