ios 如何向文本字段添加底部边框和侧面设计

标签 ios objective-c uitextfield

我想像下图一样添加底部边框,我已经成功添加了底线,但我没有得到侧边小线

enter image description here

这是我的代码

CALayer *border = [CALayer layer];
CGFloat borderWidth = 1;
border.borderColor = [UIColor lightGrayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.cornerRadius=30;
textField.layer.masksToBounds = YES;

最佳答案

您需要添加 3 个图层(底部、左侧和右侧)。检查下面的代码。

CALayer *bottomBorder = [CALayer layer], *leftBorder = [CALayer layer], *rightBorder = [CALayer layer];

CGFloat thickness = 1.0f;
CGFloat side_height = 6.0f;

leftBorder.frame = CGRectMake(0, textField.frame.size.height - side_height, thickness, textField.frame.size.height - 1);
rightBorder.frame = CGRectMake(textField.frame.size.width - 1, textField.frame.size.height - side_height, thickness, textField.frame.size.height - 1);
bottomBorder.frame = CGRectMake(0, textField.frame.size.height-1, textField.frame.size.width, thickness);

bottomBorder.backgroundColor = [UIColor lightGrayColor].CGColor;
leftBorder.backgroundColor = [UIColor lightGrayColor].CGColor;
rightBorder.backgroundColor = [UIColor lightGrayColor].CGColor;

[textField.layer addSublayer:bottomBorder];
[textField.layer addSublayer:leftBorder];
[textField.layer addSublayer:rightBorder];

关于ios 如何向文本字段添加底部边框和侧面设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42350548/

相关文章:

ios - 在按Return或Done键后将文本保存在UITextField上

ios - 为什么在完成 block 中更改 .text 字段时 UITextField 和 UILabel 不更新?

objective-c - NSNumberFormatter 不允许十进制输入

ios - 向 ios 发送推送通知以与离线用户聊天,openfire xmpp

ios - Swift:使用 TableView 外部的按钮并检查选择了哪一行

iphone - UIImage encodeWithCoder 识别选择器?

ios - 执行 segue : -[UITextField length]: unrecognized selector sent to instance 时出错

ios - 从 1 个文本字段编辑 2 个标签

ios - 在 Swift 中检测到碰撞时更改图像

objective-c - IOS View 改变