答案 0 :(得分:0)
我找到了这个解决方案,它对我有用。
按照以下步骤自定义UITextField
,例如使用子类的android textfield。
UITextField
对于UITextField
下方的边框:根据需要设置tint color
和lineWidth
。
override var tintColor: UIColor! {
didSet {
setNeedsDisplay()
}
}
override func draw(_ rect: CGRect) {
let startingPoint = CGPoint(x: rect.minX, y: rect.maxY)
let endingPoint = CGPoint(x: rect.maxX, y: rect.maxY)
let path = UIBezierPath()
path.move(to: startingPoint)
path.addLine(to: endingPoint)
path.lineWidth = 2.0
tintColor.setStroke()
path.stroke()
}
指定UItextFieldDelegate
输入以下代码,询问该子类的代表
override func awakeFromNib() {
self.delegate = self
}
同时输入文字字段delegate
方法textFieldDidBeginEditing
和textFieldDidEndEditing
。
//MARK: - Textfield Delegate
func textFieldDidBeginEditing(_ textField: UITextField) {
for view in (textField.superview?.subviews)!{
if view is UILabel && view.tag == 0{
let lbl = view as! UILabel
lbl.isHidden = false
textField.placeholder = ""
}
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if newdelegate != nil{
newdelegate?.textFieldDidEndEditing!(textField)
}
if textField.text?.length == 0{
for view in (textField.superview?.subviews)!{
if view is UILabel && view.tag == 0{
let lbl = view as! UILabel
lbl.isHidden = true
textField.placeholder = lbl.text
}
}
}
}
在storyboard
中制作结构并将此subclass
分配给此UItextField
希望它对你有所帮助 谢谢你。 :)
答案 1 :(得分:0)
之后在swift文件中添加以下方法:
func textViewUnderline(textField: UITextField)**
{
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.yourColorforBorderHere.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.masksToBounds = true
textField.layer.addSublayer(border)
}
通过将viewDidLoad中的uitextfield传递给:
来调用此方法override func viewDidLoad()
{
super.viewDidLoad()
textViewUnderline(textField: yourUITextField)
}