How to make a hairline in Swift

2

Sketch a bit in Swift I find that I do not know which is the control to make the following type of model

so far I have

My question would be how to imitate the image above

Add as you told me a view of 1 height but the view is far below

it is the first of all the one that the bottom is like in white

but the line appears at the bottom

Apart from this I get the color of the edittext some way to remove it?

    
asked by Bruno Sosa Fast Tag 05.04.2018 в 14:24
source

2 answers

6

It is simple to add a subview to act as a line. Here is a guide example:

Swift 4

 let lineView = UIView(frame: CGRect(x: 0, y: 100, width: 320, height: 1.0))
 lineView.layer.borderWidth = 1.0
 lineView.layer.borderColor = UIColor.black.cgColor
 self.view.addSubview(lineView)

Objective C

UIView * lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 100,320,1)];
lineview.layer.borderColor = [UIColor blackColor].CGColor;
lineview.layer.borderWidth = 1.0;
[self.view addSubview:lineview];

Or you can check this link to add CALayer or draw a view

how to draw a line programmatically from a view controller? (Info in English).

    
answered by 05.04.2018 / 14:45
source
1

Use this UIView extension

  public enum UIButtonBorderSide {
     case Top, Bottom, Left, Right, All
  }
extension UIView {

       public func addBorder(side: UIButtonBorderSide, color: UIColor,     width: CGFloat) {
    let border = CALayer()
    border.name = "border"
    border.backgroundColor = color.cgColor

    switch side {
    case .Top:
        border.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: width)
        self.layer.addSublayer(border)
    case .Bottom:
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
        self.layer.addSublayer(border)
    case .Left:
        border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
        self.layer.addSublayer(border)
    case .Right:
        border.frame = CGRect(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)

        self.layer.addSublayer(border)
    case .All:
        let topBorder = CALayer()
        let bottomBorder = CALayer()
        let rightBorder = CALayer()
        let leftBorder = CALayer()

        topBorder.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: width)
        topBorder.backgroundColor = color.cgColor

        bottomBorder.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
        bottomBorder.backgroundColor = color.cgColor

        leftBorder.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
        leftBorder.backgroundColor = color.cgColor

        rightBorder.frame = CGRect(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
        rightBorder.backgroundColor = color.cgColor


        self.layer.addSublayer(topBorder)
        self.layer.addSublayer(bottomBorder)
        self.layer.addSublayer(leftBorder)
        self.layer.addSublayer(rightBorder)

    }


  }

}

And you use it this way

miTextField.addBorder(side: .Bottom, color: UIColor.blue, width: 2.0)
    
answered by 05.06.2018 в 19:51