Suhas Suhas - 1 year ago 69
Swift Question

Subview not resizing proportionate to super view

I have a gameView(Just a blue color view with grid lines). I have pinned this gameView to the bottom and the sides of the screen and set the height to 475. When a user taps on this view, I locate the point and create a custom view(fenceView) with the origin and frame of the cell(width and height). In the init method of the fenceView I create a new UIImageView of a fence image and add it as a subview.
Potrait View

Because of autolayout, when the device is rotated, the width of the gameView changes accordingly. But the problem is, the added fenceViews do not resize. I tried to set the autoresizingMask = [.FlexibleWidth, .FlexibleHeight] for both the gameView and the fenceView. But it did not solve the issue.
Landscape mode

We can see that in the landscape mode, the fence images are not displayed properly. It might be small mistake that I am doing, but I am not able to solve this issue since two days. Any help is greatly appreciated.

Answer Source

.FlexibleHeight does not need to be used since the overall height will always be 475. Try setting the autoresizingMask = [.FlexibleWidth, .FlexibleLeftMargin, .FlexibleRightMargin].

The width will need to change as well as its X position for the larger/smaller width. Both flexible Left and Right margin will apply the increased width to both sides.