Question

Figure 1

My app is built on a UIScrollview with a UIImageView subview and various other subviews. When I rotate to landscape, I change the contentSize of the scroll view and resize the image view proportionally to take advantage of the increased horizontal width. The means the height increases as well to maintain the proportions.

My question is, in the case of the blue subview shown, what do I need to do to reposition it such that it maintains it relative position after rotation, given that it's superview is no longer the same size? I have experimented with convertRect:toView: and converPoint:toView:, but I can't seem to get it quite right.

Was it helpful?

Solution

Are you using auto layout? If so, in many cases, the judicious use of constraints can keep that subview in the right place and right size, even as you go from landscape to portrait. But you'd have to share more details about what else is on this view for us to be more specific.

If not using auto layout, you generally can set the view's autosizing mask so it moves to the correct location for you. But in the case of a scroll view subview, you might have your view controller can respond to viewWillLayoutSubviews, updating the contentSize of the scroll view and the frame of the subview to move, accordingly:

- (void)viewWillLayoutSubviews
{
    // update the contentSize of the scroll view for the width of the root view, but I'm assuming the
    // height won't change

    self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.scrollView.contentSize.height);

    // adjust the frame of the subview you want to move so that it is a certain offset from the bottom
    // left corner of the scroll view's `contentSize` (in this case, 10 points from bottom, 10 points from right)

    self.subviewToMove.frame = CGRectMake(self.scrollView.contentSize.width  - self.subviewToMove.frame.size.width  - 10,
                                          self.scrollView.contentSize.height - self.subviewToMove.frame.size.height - 10,
                                          self.subviewToMove.frame.size.width, self.subviewToMove.frame.size.height);
}

The specifics vary based upon details of (a) whether you're using autolayout or not; (b) whether you're creating this subview programmatically or not; and (c) what other content you have in your view and whether the change from portrait to landscape and back results in any change in the vertical height of the scroll view.

OTHER TIPS

To reposition a view, you update its frame. The frame property is of type CGRect, which is a combination of size (CGSize) and origin (CGPoint). If size of your blue view doesn't change, then only origin should be updated.

iOS coordinate system starts from top left corner:

ios coordinate system

For your blue view you calculate it's origin from the bottom right corner, that is

origin = contentSize - blueViewSize - padding

Do this separately for x and y coordinate, make CGRect with updated origin, and update blue view's frame.

UP: This is how you do it manually, but you can (and better should) let UIKit reposition subviews for you automatically -- learn about autoresizing and autolayout in Xcode's Interface Builder, and Developer manuals

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top