scrollToItemAtIndexPath before the view is shown the first time - ios

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) –

Related

UIScrollView Resizing Itself

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.

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.

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.

Wrong size from UITableView.SizeThatFits()

I implemented an UIPopoverController which is filled with a grouped UITableView. The number of cells in the table view can change dynamicly as long the popover is invisible. As soon as the popover becomes visible, the number of cells are fixed.
When the popover becomes visible, i resize the popover content to the height of the table view. I use the SizeThatFits() method from the UITableView to get the table size and use this value to set the popover PopoverContentSize height.
This works fine when the app is running. But always the first time the popover is shown after a new start of the app, SizeThatFits() delivers a wrong height (about 60 pixel to high). Has anyone an idea what this problem could be?
I had a somewhat similar issue because I was performing my calculations in ViewDidLoad. I was able to work around the issue by creating a bool flag in the view's code and only performing the calculations in ViewDidAppear if the flag was not set (and, of course setting the flag so that the logic wasn't repeated each time).
On iOS 5 and up, sizeThatFits on a UITableView gives the correct result when called within the viewDidLayoutSubviews UIViewController method.

iPad orientation issue. When does the app begin to know about its orientation?

I am implementing an application which is locked to landscape only, containing a UIScrollView (the super view to add sub-views) and a UIPageControl.
When I trying to add a sub-view in viewDidLoad, calculating its frame by referencing its super view's frame, it is always portrait.
But the sub-view added later (say in the scrollViewDidScroll, when user swipes to next page) is landscape.
I set a break point and found that the super view's size is 768*969 in viewDidLoad and 1024 * 713 later.
I suspect it is because the app doesn't know about the orientation at all when viewDidLoad thus everything is default to portrait layout. Am I right?
If so, when will the app know about the orientation for the first time?
Thanks.
The workaround I found was to reparent the scroll view under an intermediate view
VC
view
view (intermediate view)
scrollview
Then the frame of the scroll view was correct in viewdidload (previously without the intermediate view, the frame was in portrait right up until viewdidappear!)

Resources