[英]How to add bottom line to multiple UITextField in subviews by swift?
我需要在子視圖中向所有文本字段添加底線。
這是我的代碼。
for view in self.detailView.subviews as! [UIView] {
if let textField = view as? UITextField {
// if textField.text == "" {
// // show error
// return
// }
var border = CALayer()
var width = CGFloat(1.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width: textField.frame.size.width, height: textField.frame.size.height)
border.borderWidth = width
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
return
}
}
但它只會將底線添加到最后一個文本字段,而不是全部。 這就是我得到的
我怎樣才能解決這個問題? 謝謝!
您已在最后添加return關鍵字。
嘗試將整個視圖及其子視圖循環為
for view in self.view.subviews as! [UIView] {
if let textField = view as? UITextField {
var border = CALayer()
var width = CGFloat(1.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width: textField.frame.size.width, height: width)
border.borderWidth = width
textField.borderStyle = UITextBorderStyle.None
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
}
}
我又添加了一行代碼,代碼為textField.borderStyle = UITextBorderStyle.None
因此,如果邊框樣式在設計上有所不同,則它將以編程方式進行更改。
非常簡單易用的方法只需將文本字段的背景圖像放置在白色背景上並帶有下划線,以便使用透明背景代替白色背景
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.