简体   繁体   English

UITextField仅在顶部或底部具有拐角半径

[英]UITextField with corner radius only on top or bottom

I have a UITextField that is added to a view and I'm trying to change it's default appearance. 我有一个添加到视图的UITextField ,我正在尝试更改它的默认外观。

What I want is to change the corner radius, but only on top / bottom. 我想要的是改变角半径,但只在顶部/底部。 I'm doing so by doing this: 我是这样做的:

UIBezierPath *usernameMaskPathWithRadiusTop = [UIBezierPath bezierPathWithRoundedRect:username.bounds
                                                                    byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                                                          cornerRadii:CGSizeMake(4.0, 4.0)];

UIBezierPath *passwordMaskPathWithRadiusBottom = [UIBezierPath bezierPathWithRoundedRect:password.bounds
                                                                       byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                                                             cornerRadii:CGSizeMake(4.0, 4.0)];

CAShapeLayer *usernameMaskLayer = [[CAShapeLayer alloc] init];
usernameMaskLayer.frame = username.bounds;
usernameMaskLayer.path = usernameMaskPathWithRadiusTop.CGPath;

CAShapeLayer *passwordMaskLayer = [[CAShapeLayer alloc] init];
passwordMaskLayer.frame = password.bounds;
passwordMaskLayer.path = passwordMaskPathWithRadiusBottom.CGPath;


[username setTextAlignment:NSTextAlignmentLeft];
[username setClearButtonMode:UITextFieldViewModeWhileEditing];
usernameLayer.shadowOpacity = 0.0;
[usernameLayer setMask:usernameMaskLayer];

[password setTextAlignment:NSTextAlignmentLeft];
[username setClearButtonMode:UITextFieldViewModeWhileEditing];
passwordLayer.shadowOpacity = 0.0;
[passwordLayer setMask:passwordMaskLayer];

Where UITextField *username , UITextField *password and the CAShapeLayer *usernameLayer = username.layer and CAShapeLayer *passwordLayer = password.layer are defined as described. 其中UITextField *usernameUITextField *passwordCAShapeLayer *usernameLayer = username.layerCAShapeLayer *passwordLayer = password.layer的定义CAShapeLayer *passwordLayer = password.layer

The issue is that after I perform that above, I get no fields at all, actually they are there but everything is transparent, text and background color. 问题是,在我执行上述操作之后,我根本没有任何字段,实际上它们在那里但是一切都是透明的,文本和背景颜色。 I'm not sure how to get that back or how to fix whatever I'm doing wrong. 我不知道如何得到回来或如何解决我做错的事情。

How exactly can I do what I'm trying right ? 我怎么能做我正在尝试的事情呢?

It seems like what I had to do is instead of setting a mask I had to add as sublayer: 看起来我要做的不是设置一个我必须添加为子层的掩码:

[username.layer addSublayer:usernameMaskLayer];

// instead of the old

[usernameLayer setMask:usernameMaskLayer];

And then for setting a background I just had to setFillColor on the usernameMaskLayer . 然后设置一个背景我刚setFillColorusernameMaskLayer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM