Ofer Magen Ofer Magen - 1 year ago 68
iOS Question

View shows sub view outside of it in swift

in swift when you define a view with a frame and insert a subview into it (for example a label) the subview will be shown even if it is outside the view's frame. How can I make sure that the view will show only the subviews that are in the frame? (The reason I want that is that I want to animate a view with subviews and make it disappear but the subview stays)

Answer Source

Set clipToBounds = true. The default value is false which means the subviews are not clipped. Note that there is a small performance penalty to doing this as it requires extra compositing.

According to the documentation:

Setting this value to true causes subviews to be clipped to the bounds of the receiver. If set to false, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is false.