我目前有一个常规边界。我想只有一个顶部和底部边框。
我如何做到这一点?
使用UITextField
的{{1}}属性,我有以下代码:
layer
我可以通过让我的UITextField超长来让它工作,这样用户就看不到左右边框,但我只是想知道是否有更好的,更少的这种做法的hackish方式?
我检查了文档,更改 self.layer.borderColor = [[UIColor colorWithRed:160/255.0f green:160/255.0f blue:160/255.0f alpha:1.0f] CGColor];
self.layer.borderWidth = 4.0f;
的{{1}}没有此选项。
从,
iOS优先计时器
答案 0 :(得分:75)
我发现一种方法很好用的是使用图层。这是一个片段:
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];
希望这有助于某人。
答案 1 :(得分:39)
@ user3075378&great;& amp; 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)
答案 2 :(得分:22)
编辑:在自动布局为视图设置正确的框架之前绘制线条存在问题,我使用修复程序编辑了我的答案:它主要涉及在{中调用class MyClass {
// ???
publicFunction () {
return 1;
}
}
{1}}:
drawLines()
答案 3 :(得分:9)
您可以创建一个带有顶部和底部边框的图像,并将其设置为UITextField的背景:
yourTextField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourBorderedImageName"]];
答案 4 :(得分:5)
您还可以在顶部和底部添加视图以用作边框。
// 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];
答案 5 :(得分:4)
您可以使用图层向任何UIView子类添加线条/形状。此代码在文本字段的顶部和底部绘制两行。您可以将其添加到控件的子类,或直接在父视图/视图控制器中调用它。
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];
答案 6 :(得分:2)
斯威夫特3: 使用AutoLayout清理(我在这里使用PureLayout,但您也可以使用常见的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)
}
答案 7 :(得分:1)
我建议您在文本字段的左侧放置一个视图,在文本字段的右侧放置一个视图以覆盖左/右边框。
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)];
答案 8 :(得分:1)
希望这会有所帮助,将其置于文本字段覆盖类
中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]];
答案 9 :(得分:0)
感谢user3075378。我的回答是基于他的。添加左右小线。
- (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];
}
答案 10 :(得分:0)
已为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)