TableView 中 Swift TextField 的底部边框宽度
Bottom Border Width on Swift TextField in TableView
我构建了一个静态表视图,其行数比屏幕多,因此用户必须滚动才能查看所有单元格。
每个单元格都有一个文本字段,其中 class 添加底部边框:
class TextFieldWithBottomBorder: UITextField {
let border = CALayer()
let width = CGFloat(1.0)
func addBottomBorder(color: UIColor){
self.border.borderColor = color.cgColor
self.border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height:self.frame.size.height)
self.border.borderWidth = self.width
self.layer.addSublayer(self.border)
self.layer.masksToBounds = true
}
func changeBorderColor(color: UIColor){
self.border.borderColor = color.cgColor
}
}
然后我在从服务器 e 接收到一些数据后调用该方法。 g.
self.firstnameTextField.text = firstNameFromDB
self.firstnameTextField.addBottomBorder(color: .blue)
这适用于当前显示的每个单元格。但是当前视图之外的单元格比文本字段短。
请参阅此屏幕截图,对于 "Vorname",意味着 firstName 一切看起来都不错,但对于电子邮件、密码等,边框太短了。
http://share-your-photo.com/34b5e80253
将此 class 用于底行文本字段
@IBDesignable class BottomTextField: UITextField {
var lineView = UIView()
@IBInspectable var lineViewBgColor:UIColor = UIColor.gray{
didSet {
if !isFirstResponder {
lineView.backgroundColor = lineViewBgColor
}
}
}
required init?(coder aDecoder:NSCoder) {
super.init(coder:aDecoder)!
setup()
}
override init(frame:CGRect) {
super.init(frame:frame)
setup()
}
// MARK:- Private Methods
private func setup() {
lineView.frame = CGRect(x:CGFloat(0), y:self.frame.size.height-2, width:self.frame.size.width, height:CGFloat(1))
lineView.backgroundColor = lineViewBgColor
self.addSubview(lineView)
}
}
看起来 UITextField 的大小在您调用 addBottomBorder 后正在调整大小,因此在行中使用的 UIView 现在不够宽。很难说为什么在没有看到更多代码的情况下会出现这种情况,但是您可以使用多种方法来克服它。
1) 切换到 UIView 而不是 CALayer 并使用自动布局使视图保持在正确的位置。
2) 覆盖layoutSubviews更新底线的frame
对您来说最简单的可能是选项 2(尽管我会选择选项 1),它看起来像这样:
override func layoutSubviews() {
super.layoutSubviews()
self.border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height:self.frame.size.height)
}
现在,每当文本字段的 frame/size 更改时,边框线 CALayer 的 frame/size 将相应更新。
我构建了一个静态表视图,其行数比屏幕多,因此用户必须滚动才能查看所有单元格。
每个单元格都有一个文本字段,其中 class 添加底部边框:
class TextFieldWithBottomBorder: UITextField {
let border = CALayer()
let width = CGFloat(1.0)
func addBottomBorder(color: UIColor){
self.border.borderColor = color.cgColor
self.border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height:self.frame.size.height)
self.border.borderWidth = self.width
self.layer.addSublayer(self.border)
self.layer.masksToBounds = true
}
func changeBorderColor(color: UIColor){
self.border.borderColor = color.cgColor
}
}
然后我在从服务器 e 接收到一些数据后调用该方法。 g.
self.firstnameTextField.text = firstNameFromDB
self.firstnameTextField.addBottomBorder(color: .blue)
这适用于当前显示的每个单元格。但是当前视图之外的单元格比文本字段短。 请参阅此屏幕截图,对于 "Vorname",意味着 firstName 一切看起来都不错,但对于电子邮件、密码等,边框太短了。 http://share-your-photo.com/34b5e80253
将此 class 用于底行文本字段
@IBDesignable class BottomTextField: UITextField {
var lineView = UIView()
@IBInspectable var lineViewBgColor:UIColor = UIColor.gray{
didSet {
if !isFirstResponder {
lineView.backgroundColor = lineViewBgColor
}
}
}
required init?(coder aDecoder:NSCoder) {
super.init(coder:aDecoder)!
setup()
}
override init(frame:CGRect) {
super.init(frame:frame)
setup()
}
// MARK:- Private Methods
private func setup() {
lineView.frame = CGRect(x:CGFloat(0), y:self.frame.size.height-2, width:self.frame.size.width, height:CGFloat(1))
lineView.backgroundColor = lineViewBgColor
self.addSubview(lineView)
}
}
看起来 UITextField 的大小在您调用 addBottomBorder 后正在调整大小,因此在行中使用的 UIView 现在不够宽。很难说为什么在没有看到更多代码的情况下会出现这种情况,但是您可以使用多种方法来克服它。
1) 切换到 UIView 而不是 CALayer 并使用自动布局使视图保持在正确的位置。
2) 覆盖layoutSubviews更新底线的frame
对您来说最简单的可能是选项 2(尽管我会选择选项 1),它看起来像这样:
override func layoutSubviews() {
super.layoutSubviews()
self.border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height:self.frame.size.height)
}
现在,每当文本字段的 frame/size 更改时,边框线 CALayer 的 frame/size 将相应更新。