gpt4 book ai didi

iOS - UITextField : extend the bottom line for ANY resolution

转载 作者:行者123 更新时间:2023-12-01 19:55:33 25 4
gpt4 key购买 nike

我用 创建了一个 UITextFiled底线使用这个:

let Bottomline CALayer = ()
bottomLine.frame CGRect = (x: 0, y: usernameTextField.frame.height-7, width: usernameTextField.frame.width, height: 1)
bottomLine.backgroundColor = UIColor.black.cgColor
TextField.borderStyle = UITextBorderStyle.none
TextField.layer.addSublayer (Bottomline)

iPhone 6(右)的结果是这样的:



行。

✄------------------------

The problem is to run the same application on a Pro iPad, because the bottom line does not extend following the UITextField, but is shorter



这是 iPad Pro 上的结果:

enter image description here

我不明白为什么底线不遵循 UITextField。当我调用底线时,我定义为:
bottomLine.frame CGRect = (x: 0, y: usernameTextField.frame.height-7, width: usernameTextField.frame.width, height: 1)

我已指定底部线的长度必须为:
width: usernameTextField.frame.width

有什么问题?

EDIT 1: The contrains are correct, because the UITextField adapts to all types of resolution



enter image description here

编辑:2谢谢马特!现在工作!!!

enter image description here

最佳答案

I do not understand why the bottom line does not follow the UITextField



因为它是一层。当它们的超层(文本字段)改变大小时,图层不会自动改变大小。

因此,每次文本字段更改大小时,您都需要重新绘制底线。

不过,此时您正在 viewDidLoad 中配置“底线”层。 .所以你基于 frame文本字段在那一刻拥有。但是文本字段尚未达到其实际大小。然后它确实改变了大小,同时你的“底线”层就在那里——所以现在它的大小是错误的。

一个简单的解决方案是子类化 UITextField 并每次重绘线条 layoutSubviews叫做:
class MyTextField : UITextField {
var lineLayer : CALayer?
override func layoutSubviews() {
super.layoutSubviews()
self.lineLayer?.removeFromSuperlayer()
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0, y: self.bounds.height-7, width: self.bounds.width, height: 1)
bottomLine.backgroundColor = UIColor.black.cgColor
self.layer.addSublayer(bottomLine)
self.lineLayer = bottomLine
}
}

如果您的文本字段是 MyTextField,它将完全按照您的意愿行事。

关于iOS - UITextField : extend the bottom line for ANY resolution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42888437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com