UIScrollView Resizing Itself - ios

I have a UIScrollView inside of a VC. In my viewDidLoad I call a method to set the content size. The issue, however, is that it sets the contentSize to 638, but that immediately gets set to 800. As soon as I rotate, I call that method again and it corrects itself to be 638.
So, something is happening in the loading of my VC. Additionally, if I delay that method call and call it .01 seconds after my viewDidLoad, it works fine and dandy.
Does anyone know anything inside of iOS 7 that would cause a scroll view to resize itself?
More Info: None of my super classes are touching it. They have a property for a scroll view, but I can remove that property and it builds just fine. So this is the only class I've written that touches the contentSize of the scroll view.
This does not happen on iOS 8, only on iOS 7.

i dont know why this is happing but exactly this was happening in my app as well.
You can fix it by calling your method in both viewDidLoad and ViewWillAppear.

Related

scrollToItemAtIndexPath before the view is shown the first time

I have a UIView where I added a UICollectionView. Now this view is shown on app start up. At the same time I'm scrolling to a certain position in my collection view. Therefore I use scrollToItemAtIndexPath. The problem now is that it scrolls to a wrong position, because the collection view has the wrong size at the beginning: If you start the iPad in the landscape orientation on iOS 7 the collection view always takes 768 as width despite it is in landscape.
How can I scroll to a certain position without the user noticing it? Where in the view hierarchy can I call it?
viewWillAppear: frames have not been set
viewDidAppear: not possible without the user noticing it
layoutSubviews: this is called multiple times and I only need it at startup and when the user presses a button
I also tried to use layoutIfNeeded, but that doesn't help for my edge case (iOS 7, iPad, landscape at startup).
Constraints kick around the time viewDidAppear and layoutSubviews is called for the last time. By that time it's too late.
You can try inheriting your own class from UICollectionView and see if its layoutSubviews has correct constraints and call scrollToIndex from there.
If that fails, you can cheat by setting frame sizes in viewDidLoad and calling scroll to index. ( This one is a tad bit more complicated) –

viewDidLayoutSubviews called after touchesBegan - again and again

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):
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:
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;
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.

Why UIScrollView can not scroll once dragging UIButton or UILabel from storyboard

I got the same issue here
The UIScrollView can scroll when adding button or label via code, but failed when adding from the storyboard or xib.
Setting the contentSize from the viewDidAppear can resolve this issue.
Set up the contentSize from viewDidLoad can not work. Question here is to figure out why ? I hope someone can explain the insider magics or this is just a bug of UIScrollView. I think it should not be designer like this way.
The reason it doesn't work in ViewDidLoad is that all the sizes are not yet final there, in fact, Im betting it would also not work in viewWillAppear.Im guessing you are not liking making the change in viewDidAppear as it is visible to the user. The correct place to set the size is viewWillLayoutSubviews.
The flow is
viewDidLoad
viewWillAppear
viewDidLayoutSubviews
viewDidApear
I suggest you reread those methods in the documentation.

UIView - Subview contents aren't redrawn until subviews are laid out again. Why?

I am adding some subviews to my view controller's root view in the viewDidLoad method. Soon after, when a particular event occurs, I am simply changing the background color on one of the views. This change isn't visible until the subviews are laid out again (willLayoutSubviews gets called) This happens either if I change the orientation of the device or wait 10-20 seconds. I am calling setNeedsDisplay on the subview right after making the change, but it is not helping.
To summarize, I'm simply trying to change the background color of a subview after a certain event occurs, and I'd like the changes to be visible on screen within maybe a second or so. What am I missing?
Note: In case it matters, the superview of the view in question is a GLKView.
Make sure you are only making UI updates from the main thread. You can fix this by using performSelectorOnMainThread:withObject:waitUntilDone: or dispatch_async(dispatch_get_main_queue(), ^{/* your UI modifying code here */});.

Scrolling function called simultaneously while rotating the ipad screen

I am creating a UIscroll view with 8 pages. Each page is another view controller class with some functionality which is added to this scrollview.
On loading the app, when I scroll the screens from one page to another,the app just works fine.
But when I rotate the ipad to a different interface orientation, the UIScrollView delegate method - ScrollViewDidScroll is called upon and the current screen(page) is scrolled to the previous screen. Debugging makes me reckon that the scrolling function is called simultaneously with the device rotation.
How to stop/prevent the 'ScrollViewDidScroll' delegate method to be called on rotation so that only the rotation functionality is performed (i.e. the screen shouldn't be moved to previous one)?
I have tried different solutions provided but with little help
I am fairly new to xcode development.
Instead of using scrollViewDidScroll method to set the current page, you can use the scrollViewDidEndDecelerating method which is not called on rotation.
With this, you just have to set right contentOffset after the rotation, based on the current page.

Resources