0
votes

I created a simple textfield to experiment with cornerRadius and maskToBounds and thought that if I wanted to round the corner of the textfield I would need to also set the layer´s maskToBounds to true in addition to setting the cornerRadius of the layer, like so:

myTextField.layer.borderWidth = 1
myTextField.layer.cornerRadius = 3
myTextField.layer.masksToBounds = true
myTextField.textAlignment = .center

However when I commented out myTextField.layer.masksToBounds = true, the textfield still presented on the screen with rounded corners.

So when exactly would it be necessary to use maskToBounds, and why is it not necessary here? The background colour of the entire screen is white and there are no images of any other elements inside the textField.

1
i think you add direct add textfield in main view so maskstobounds in not required but you add one view then add textfield inside view then maskstobounds in compulsory. "It depends on masksToBounds which determines if the sublayers are clipped to the receiver’s bounds. So If YES, an implicit mask matching the layer bounds is applied to the layer, including the effects of the cornerRadius property."Dixit Akabari
Sorry, can you rephrase that? I think you are making a good point but I can't fully understand what you are saying in the first sentence. Do you mean that maskToBounds is not needed because I am adding the textfield to the main view (which, btw, I am :)). But what do you mean with the rest "but you add one view then add textfield inside view then maskstobounds in compulsory"?cheznead
it means you add textfield inside view then you don't set masktobounds at that time textfield not present with rounded corners.Dixit Akabari
Ok, can you give me common use examples for when would you might enclose a textfield inside another view (that isn't the main view)? Feel free to put this into answer form so I can accept it. thanks for your help.cheznead

1 Answers

0
votes

I Think,

It depends on masksToBounds which determines if the sublayers are clipped to the receiver’s bounds. So If YES, an implicit mask matching the layer bounds is applied to the layer, including the effects of the cornerRadius property.

shadow is an effect done outside the View, and that masksToBounds set to YES will tell the UIView not to draw anything that is outside itself.

For Ex.

If you set that on a CALayer with image contents, the image will still be drawn outside the corner radius boundary. You can solve this by set sublayer masksToBounds to True but you don’t set masktobounds true at that time shadows don’t show up because they will be masked out.