UIScrollView Pagination on rotation - iphone

I have a UIScrollview with some subviews, on rotation I resize and all looks correct, the problem is that when I rotate even though I have correctly resized everything in viewwillrotate, my scrollview is between two pages.
A single touch on the scrollview aligns everything correctly, what am I doing wrong?

This can be fixed with setting content offset manually:
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

Related

Storyboard UIScrollView contentSize?

I feel like I have touched on every single possible cause for stopping this, but I have a UIScrollView in my Storyboard hooked up with an outlet and in the viewDidLoad I set the contentSize so that I can scroll (yes bigger than my frame size)!
However, whatever I change, I just can't scroll! I have a couple of textfields in my scrollview and bouncing enabled so I can see that when testing its moves up and down with my subviews in it but whatever I set the contentSize to I just can't scroll.
Anything I might be missing/should check? Is this a known issue with UIScrollView being used in a storyboard?
Whats even stranger is, I can do something like this:
[scrollView setBackgroundColor:[UIColor blueColor]]; and I have a blue scroll view! But setting content size fails.
Edit
My only code (otherwise scrollview is just dropped into storyboard view controller):
-(void)viewDidAppear:(BOOL)animated
{
[scrollView setContentSize:CGSizeMake(320, 640)];
}
Logged frame, comes out as expected:
width: 320.00
height: 504.00
Edit 2
Turns out that removing any subviews of the scroll view in my storyboard lets it scroll just fine. If I add any subview to it at all via the storyboard, even a blank brand new UIButton it just won't apply the contentSize/allow scrolling.
use ViewDidLayoutSubview
- (void)viewDidLayoutSubviews
{
[_scrollView setContentSize:CGSizeMake(320, 500)];
}
UIViewController's method invoke sequence is as below
awakeFromNib
viewDidLoad
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
viewDidLoad is not a good place to put code that relies on frame sizes of IB objects. If you log the contentSize of your scroll view in viewDidLoad, you will see that it's (0,0). Move the code (where you set the content size) to viewDidAppear, and it will work properly.
Check these
User Interaction enabled
Outlet connected
Included contentsize greater than bounds
scrolling Enabled
eg
scrollView.contentSize = CGSizeMake(320, 640);
My storyboard looks like this for scrollview [working]
I had exactly the same line of code in viewDidAppear and it did not work
Moved it to viewDidLayoutSubviews and it worked correctly.
[scrollView setContentSize:CGSizeMake(320, 500)];
Thanks trick14 for the answer.
The issue is most probably with Auto Layout. UIScrollView needs special attention when using AutoLayout.
Quick-fix - bind one of the scroll's subviews to the top AND bottom space of it's superview (the scroll view).
Long story:
Questions on SO:
UIScrollView not scrolling regardless of large contentSize,
UIScrollView will not scroll, even after content size set,
UIScrollView doesn't use autolayout constraints
Apple's Documentation:
https://developer.apple.com/library/ios/technotes/tn2154/_index.html
Trip14's answer worked for me. In swift I coded it as:
override func viewDidLayoutSubviews() {
(self.view as! UIScrollView).contentSize = CGSizeMake(600, 600)
}
This seems to be a similar issue. Other Story
It might be an issue with auto layout or constraints in the storyboard.
the best way with the storyboard.:

Change view alternative

I have an window based app with some ViewController.
I switch from one view to another using :
[self.view addSubview:newViewController.view];
Everything works fine but I have a problem:
When I change the view to a scrollView which has frames more than 320x480 I can not return to previous frames.I mean I change the the views further normally but I have a bigger frame and I see the rest of the content from the scrollView which is not covered by the current view.
Is there any method to set the superview size back to 320x480?
Thanks
You can set the frame of a view back to 320X480 manually by doing something like:
yourView.frame = CGRectMake(0,0,320,480);
When you switch back are you removing the scrollview from the superview as well? [scrollview removeFromSuperView]; ?

How to scroll with UIScrollView with stopping only at pecific regular positions?

I have image that needs to be scrolled in uiscrollview but scrolling needs to be stopped only at specific points. Imagine I have long image of uitableview so I need to scroll it. What I'm thinking about: is it possible to stop scroll only at points where full row is visible at the top? If yes, then how to do it?
You need to set the coordinates to where you want the scroll to move. This is done here -
[scrollView scrollRectToVisible:CGRectMake(0, 50, 1536, 939) animated:NO];
So you need to specify the exact CGRect specs you need. But this will work for you.
If you turn animated:YES then the scroll to this new position is smoothly animated.

How to load a view that is larger than a specific area in Xcode 4?

I"m relatively new to iOS development, as you can likely tell, and would like to display about 600 pixels worth of content (in height) on the screen. Just to give you a bit of context, I'm building a tab bar application, so what I'm assuming would be a UIScrollView cannot be defined in the App Delegate. Primarily, what I'm confused about is changing the size of the view and then defining the behaviour of the UIScrollView. I've looked everywhere, but they are all tutorials for < Xcode 4, plus none of them use the View Controller to define a scroll view. Is there a specific IBAction that would only allow the view to scroll up and down, not left and right. Any help would be appreciated.
Once you define the contentSize and assign a subview to the UIScrollView you should be able to scroll automatically.
To make sure you have vertical and horizontal scrolling you could do this -
[scrollview setBounces:YES];
[scrollview setShowsVerticalScrollIndicator:YES];
[scrollview setShowsHorizontalScrollIndicator:YES];
[scrollview setScrollEnabled:YES];
[scrollview setUserInteractionEnabled:YES];
If you want to add double-tap to zoom etc. you could do that too in similar way...

UIScrollView Updating contentSize Causes Scroll

I have a UIScrollView that has a single child view within it. If I set the contentSize of the UIScrollView immediately after creation, everything works as I expect and I get scrolling.
The challenge is the view within the UIScrollView is dynamically sized. The width is fixed, but the height is unknown at the time I set up the scrollview.
When I do a [scrollView setContentSize:CGRectMake(...)] after the inner view does it's thing, the scrollview updates to the proper size and scrolling works. So basic scrolling works fine.
However, the major problem is that when I setContentSize at a later point, the UIScrollView decides to scroll down(with animation) towards the end of the scrollview, which is not what I want, I want the scroll to stay at the top, and let the contents within the scrollview either get bigger or smaller, without changing the apparent scroll position.
What am I missing?
Why don't you also call [scrollview setContentOffset:CGPointMake(0,0)]; when you call the setContentSize?
To force UIScrollView to scroll only e.g. horizontally, I always set non-scrollable value for contentSize to 0, using:
CGSizeMake(contentSize.width, 0).