4

我有一个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 *username和和UITextField *password_CAShapeLayer *usernameLayer = username.layerCAShapeLayer *passwordLayer = password.layer如前所述。

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

我到底该怎么做我正在尝试的事情?

4

1 回答 1

5

似乎我必须做的不是设置我必须添加为子层的蒙版:

[username.layer addSublayer:usernameMaskLayer];

// instead of the old

[usernameLayer setMask:usernameMaskLayer];

然后为了设置背景,我只需要setFillColorusernameMaskLayer.

于 2013-11-12T08:21:17.947 回答