2017-02-20 77 views
0

我想补充的底边框像下面的图像,我已经成功地将botom线,但我没有得到侧小线IOS如何添加边框底部和侧面设计到文本框

enter image description here

这里是我的代码

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; 

回答

1

您需要添加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]; 
1

我刚刚写了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!) 
     } 
    } 
} 
相关问题