UITextField Only Top And Bottom Border

后端 未结 11 963
温柔的废话
温柔的废话 2020-12-07 22:46

I currently have a regular border. I would like to only have a top and bottom border.

How do I accomplish this?

Using the UITextField<

相关标签:
11条回答
  • 2020-12-07 23:36

    Hope this helps, put this inside a textfield override class

    UIView *view = [[UIView alloc] init];
            view.translatesAutoresizingMaskIntoConstraints = NO;
            view.layer.borderWidth = 1;
            view.backgroundColor = [UIColor blackColor];
            [self addSubview:view];
    
            [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
            [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
            [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1.0]];
            [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:1.0]];
    
    0 讨论(0)
  • 2020-12-07 23:40

    One approach I have found works good is using layers. Here's a snippet:

    CALayer *bottomBorder = [CALayer layer];
    bottomBorder.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
    bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
    [myTextField.layer addSublayer:bottomBorder];
    

    Hope this helps someone.

    0 讨论(0)
  • 2020-12-07 23:50

    You can use layers to add lines / shapes to any UIView subclass. This code draws two lines at the top and bottom of a text field. You can add it to a subclass of a control, or call this directly in a parent view / view controller.

    CGRect layerFrame = CGRectMake(0, 0, _usernameField.frame.size.width, _usernameField.frame.size.height);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, layerFrame.size.width, 0); // top line
    CGPathMoveToPoint(path, NULL, 0, layerFrame.size.height);
    CGPathAddLineToPoint(path, NULL, layerFrame.size.width, layerFrame.size.height); // bottom line
    CAShapeLayer * line = [CAShapeLayer layer];
    line.path = path;
    line.lineWidth = 2;
    line.frame = layerFrame;
    line.strokeColor = [UIColor blackColor].CGColor;
    [_usernameField.layer addSublayer:line];
    
    0 讨论(0)
  • 2020-12-07 23:52

    @Sebyffffd why stop there? (;

    EDIT: There is an issue with lines being drawn before auto layout sets the right frame for the view, I edited my answer with a fix: it basically involves calling drawLines() in layoutSubviews():

    class FramedTextField: UITextField {
    
        @IBInspectable var linesWidth: CGFloat = 1.0 { didSet{ drawLines() } }
    
        @IBInspectable var linesColor: UIColor = UIColor.blackColor() { didSet{ drawLines() } }
    
        @IBInspectable var leftLine: Bool = false { didSet{ drawLines() } }
        @IBInspectable var rightLine: Bool = false { didSet{ drawLines() } }
        @IBInspectable var bottomLine: Bool = false { didSet{ drawLines() } }
        @IBInspectable var topLine: Bool = false { didSet{ drawLines() } }
    
    
    
        func drawLines() {
    
            if bottomLine {
                add(CGRectMake(0.0, frame.size.height - linesWidth, frame.size.width, linesWidth))
            }
    
            if topLine {
                add(CGRectMake(0.0, 0.0, frame.size.width, linesWidth))
            }
    
            if rightLine {
                add(CGRectMake(frame.size.width - linesWidth, 0.0, linesWidth, frame.size.height))
            }
    
            if leftLine {
                add(CGRectMake(0.0, 0.0, linesWidth, frame.size.height))
            }
    
        }
    
        typealias Line = CGRect
        private func add(line: Line) {
            let border = CALayer()
            border.frame = line
            border.backgroundColor = linesColor.CGColor
            layer.addSublayer(border)
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            drawLines()
        }
    
    }
    
    0 讨论(0)
  • 2020-12-07 23:52

    Updated for Swift 5

    var bottomBorder = CALayer()
    bottomBorder.frame = CGRect(x: xVal, y: yVal, width: w, height: h)
    bottomBorder.backgroundColor = UIColor.black.cgColor
    searchBar.layer.addSublayer(bottomBorder)
    
    0 讨论(0)
提交回复
热议问题