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<
@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)
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];
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"]];
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];
}
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)
}
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)];