ios - 仅在顶部或底部具有圆角半径的 UITextField

标签 ios objective-c ios7

我有一个添加到 View 的 UITextField,我正在尝试更改它的默认外观。

我想要的是改变拐角半径,但仅限于顶部/底部。我正在这样做:

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];

其中 UITextField *usernameUITextField *passwordCAShapeLayer *usernameLayer = username.layerCAShapeLayer *passwordLayer = password .layer 按照描述定义。

问题是,在执行上述操作后,我根本看不到任何字段,实际上它们在那里,但一切都是透明的,包括文本和背景颜色。我不确定如何取回它或如何解决我做错的任何事情。

我怎样才能正确地做我想做的事?

最佳答案

看来我必须做的不是设置 mask ,而是必须添加为子层:

[username.layer addSublayer:usernameMaskLayer];

// instead of the old

[usernameLayer setMask:usernameMaskLayer];

然后为了设置背景,我只需要在 usernameMaskLayersetFillColor

关于ios - 仅在顶部或底部具有圆角半径的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19910361/

相关文章:

ios - 在这种情况下我应该使用应用内购买吗?

objective-c - 自定义键盘应用程序扩展 iOS 8 上的 Siri

facebook - XMPPFrameWork didNotAuthenticate 在 Facebook

ios - 自定义 UI 导航栏和按钮

iOS 7 AutoLayout "Top Space to Top Layout Guide"在 Interface Builder 中没有控制拖动?

iOS,处理未确定的JSON数据结构

IOS:如何打开 View Controller 设置?

ios - 自定义 View 不适合 UItableview 中的标题

objective-c - NSNumber 保存 double

objective-c - 当值改变时 KVO 不触发