UITextField只有顶angular或底angular的半径

我有一个UITextField被添加到视图,我试图改变它的默认外观。

我想要的是改变angular落的半径,但只在顶部/底部。 我这样做是这样做的:

 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的定义CAShapeLayer *passwordLayer = password.layer

问题是,在我执行上述操作之后,我根本没有得到任何字段,实际上他们在那里,但是一切都是透明的,文字和背景颜色。 我不知道该怎么弄回来,或者如何解决我做错的事情。

我怎么能做我正在尝试的东西?

这似乎是我所要做的,而不是设置一个面具,我不得不添加为子图层:

 [username.layer addSublayer:usernameMaskLayer]; // instead of the old [usernameLayer setMask:usernameMaskLayer]; 

然后为了设置一个背景,我只需要在setFillColor上设置setFillColor