是否可以在Ios中创建一个Textfield,它只显示底部边框线而不显示上边线和侧边线。如果是,我该如何实现这个
答案 0 :(得分:54)
是的,这是可能的。以下是如何做到这一点:
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor darkGrayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
希望这会有所帮助!!
答案 1 :(得分:19)
针对Swift 3.0进行了更新
extension UITextField {
func useUnderline() {
let border = CALayer()
let borderWidth = CGFloat(1.0)
border.borderColor = UIColor.lightGray.cgColor
border.frame = CGRect(origin: CGPoint(x: 0,y :self.frame.size.height - borderWidth), size: CGSize(width: self.frame.size.width, height: self.frame.size.height))
border.borderWidth = borderWidth
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
答案 2 :(得分:9)
夫特:
extension UITextField {
func useUnderline() {
let border = CALayer()
let borderWidth = CGFloat(1.0)
border.borderColor = UIColor.blackColor().CGColor
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height)
border.borderWidth = borderWidth
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
这样称呼:
yourtextFieldName.useUnderline()
答案 3 :(得分:0)
只需将CALayer用于文本字段,只有Ios中的底线
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, self.passwordField.frame.size.height - 1, self.passwordField.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[self.passwordField.layer addSublayer:bottomBorder];
答案 4 :(得分:0)
答案 5 :(得分:-1)
User4645956在Swift中的解决方案
private func addBottomLineToTextField(textField : UITextField) {
let border = CALayer()
let borderWidth = CGFloat(1.0)
border.borderColor = UIColor.whiteColor().CGColor
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height)
border.borderWidth = borderWidth
textField.layer.addSublayer(border)
textField.layer.masksToBounds = true
}
用法:
self.addBottomLineToTextField(usernameTextField)
self.addBottomLineToTextField(passwordTextField)