UITextField Only Top And Bottom Border

后端 未结 11 962
温柔的废话
温柔的废话 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:27

    @user3075378's great & simple example in Swift

    var bottomBorder = CALayer()
    bottomBorder.frame = CGRectMake(0.0, textField.frame.size.height - 1, textField.frame.size.width, 1.0);
    bottomBorder.backgroundColor = UIColor.blackColor().CGColor
    textField.layer.addSublayer(bottomBorder)
    
    0 讨论(0)
  • 2020-12-07 23:27

    You can also add views to the top and bottom to use as borders.

    // Top border
    UIView *topBorder = [[UIView alloc]
        initWithFrame:CGRectMake(0,
                                 0,
                                 textfield.frame.size.width,
                                 4.0f)];
    topBorder.backgroundColor = [UIColor colorWithRed:160/255.0f
                                                green:160/255.0f
                                                 blue:160/255.0f
                                                alpha:1.0f];
    [textfield addSubview:topBorder];
    
    // Bottom border
    UIView *bottomBorder = [[UIView alloc]
        initWithFrame:CGRectMake(0,
                                 textfield.frame.origin.y +
                                     textfield.frame.size.height - 4.0f,
                                 textfield.frame.size.width,
                                 4.0f)];
    bottomBorder.backgroundColor = [UIColor colorWithRed:160/255.0f
                                                   green:160/255.0f
                                                    blue:160/255.0f
                                                   alpha:1.0f];
    [textfield addSubview:bottomBorder];
    
    0 讨论(0)
  • 2020-12-07 23:28

    you can create one image that with top and bottom border and set it to the background of your UITextField :

    yourTextField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourBorderedImageName"]];
    
    0 讨论(0)
  • 2020-12-07 23:28

    Thanks to user3075378. my answer is based on his. adding right and left small lines.

    - (void)styleSearchTextField {
        CALayer *bottomBorder = [CALayer layer], *leftBorder = [CALayer layer], *rightBorder = [CALayer layer];
    
        CGFloat thickness = 1.0f;
        CGFloat side_height = 6.0f;
    
        bottomBorder.frame = CGRectMake(0.0f, searchTextField.frame.size.height - 1, searchTextField.frame.size.width, thickness);
    
        leftBorder.frame = CGRectMake(0.0f, searchTextField.frame.size.height - side_height, thickness, searchTextField.frame.size.height - 1);
    
        rightBorder.frame = CGRectMake(searchTextField.frame.size.width - 1, searchTextField.frame.size.height - side_height, thickness, searchTextField.frame.size.height - 1);
    
        bottomBorder.backgroundColor = [self.stylingDetails themeGrayColor].CGColor;
        leftBorder.backgroundColor = [self.stylingDetails themeGrayColor].CGColor;
        rightBorder.backgroundColor = [self.stylingDetails themeGrayColor].CGColor;
    
        [searchTextField.layer addSublayer:bottomBorder];
        [searchTextField.layer addSublayer:leftBorder];
        [searchTextField.layer addSublayer:rightBorder];
    }
    
    0 讨论(0)
  • 2020-12-07 23:31

    Swift 3: Clean with AutoLayout (I'm using here PureLayout but you can also do it with common NSLayoutConstraint:

    func makeUnderline(for textField: UITextField) {
        let borderLine = UIView()
        borderLine.backgroundColor = UIColor.black
        borderLine.autoSetDimension(.height, toSize: 1)
        textField.addSubview(borderLine)
        borderLine.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets.zero, excludingEdge: .top)
    }
    
    0 讨论(0)
  • 2020-12-07 23:34

    I will suggest you put one view on the left side of the textfield and one view on the right side of the textfield to cover the left/right border.

    UIView *v1 = [[UIView all] initWithFrame:CGRectMake(textfield.frame.origin.x - 5, textfield.frame.origin.y, 10, textifield.frame.size.height)];
    UIView *v2 = [[UIView all] initWithFrame:CGRectMake(textfield.frame.origin.x + textfield.frame.size.with - 5, textfield.frame.origin.y, 10, textifield.frame.size.height)];
    
    0 讨论(0)
提交回复
热议问题