Question

In an iPhone word game I have an UIScrollView (holding UIImageView) and 7 draggable custom UIViews placed initially at the bottom (and outside the scroll view):

app screenshot

In the single ViewController.m I have overwritten viewDidLayoutSubviews so that it always sets the zoomScale of the scroll view - to have the UIImageView fill exactly 100% of the screen width.

This works well - for portrait and landscape modes. And when the app is just started:

portrait

landscape

My problem is however, when I first pinch/zoom/double-tap the scroll view and then move one of the seven Tile.m views:

Suddenly (not every time) viewDidLayoutSubviews is called after touchesBegan.

This resets the zoom of the scroll view - unexpectedly for the user.

My question is if there is any way to disable this behavior?

Is it possible to prevent viewDidLayoutSubviews call of the parent, when its child UIView is being touched/dragged?

UPDATE:

I've moved the zoomScale setting code from viewDidLayoutSubviews to didRotateFromInterfaceOrientation and the scroll view zoom is okay now, but its contentOffset is reset to {0,0}, when (not always) I drag a Tile - i.e. the scroll view jumps suddenly, the app is unusable.

Adding the following to viewDidLoad hasn't helped:

if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)])
   self.automaticallyAdjustsScrollViewInsets = NO;
Was it helpful?

Solution

viewDidLayoutSubviews is called whenever the system performs layout on the view (layoutSubviews). This can be called for a plethora of reasons; you can subclass your view, implement an empty layoutSubviews method (don't forget to call the super implementation!) and put a breakpoint there to see who causes the layout. You may also want to implement setNeedsLayout and layoutIfNeeded, and put breakpoints there also for your investigation, as these trigger layout on followup runloops.

But you will never be able to prevent layout. The system performs layout on many occasions which are outside of your control. For example, if a user makes a call, exists the phone app and returns to your app; a layout is triggered because the bounds and frame of the window have changed. The call ends; layout is again triggered because now the window is back to previous size.

You should be responsible for figuring out when to set the zoom scale. For example, if the user starts a gesture, you should signal your code not to perform changes, even if a layout was performed.

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