ios如何将底部边框和侧边devise添加到文本字段

我想添加像下面的图像的底部边框,我已经成功添加了botom行,但我没有得到小小的一行

在这里输入图像说明

这是我的代码

CALayer *border = [CALayer layer]; CGFloat borderWidth = 1; border.borderColor = [UIColor lightGrayColor].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.cornerRadius=30; textField.layer.masksToBounds = YES; 

你需要添加3层(底部,左和右)。 检查下面的代码。

 CALayer *bottomBorder = [CALayer layer], *leftBorder = [CALayer layer], *rightBorder = [CALayer layer]; CGFloat thickness = 1.0f; CGFloat side_height = 6.0f; leftBorder.frame = CGRectMake(0, textField.frame.size.height - side_height, thickness, textField.frame.size.height - 1); rightBorder.frame = CGRectMake(textField.frame.size.width - 1, textField.frame.size.height - side_height, thickness, textField.frame.size.height - 1); bottomBorder.frame = CGRectMake(0, textField.frame.size.height-1, textField.frame.size.width, thickness); bottomBorder.backgroundColor = [UIColor lightGrayColor].CGColor; leftBorder.backgroundColor = [UIColor lightGrayColor].CGColor; rightBorder.backgroundColor = [UIColor lightGrayColor].CGColor; [textField.layer addSublayer:bottomBorder]; [textField.layer addSublayer:leftBorder]; [textField.layer addSublayer:rightBorder]; 

我刚刚写了UITextField的边界类,我希望它能帮助你。

 class GUTextField: UITextField { public var bottomLineView:UIView? @IBInspectable var lineColor = UIColor.gray //MARK:- UiTextfield Draw Method Override override func draw(_ rect: CGRect) { super.draw(rect) self.frame = CGRect(x:self.frame.minX, y:self.frame.minY, width:rect.width, height:rect.height) addBottomLine() } //MARK:- ADD Bottom Line private func addBottomLine(){ bottomLineView?.removeFromSuperview() bottomLineView = UIView(frame: CGRect(x:0, y:self.frame.height-1, width:self.frame.width, height:2)) bottomLineView?.backgroundColor = lineColor; bottomLineView?.isHidden = true if bottomLineView != nil { self.addSubview(bottomLineView!) } } }