UIScrollView auto-scrolls to top after SetContentSize during orientation change - how can I prevent that? - iphone

I have a UIScrollView in which vertical only scrolling is enabled. I'm displaying a grid of buttons (similar to the photo viewer image grid). The grid has to be drawn differently based on screen orientation, so that all of the screen real estate is used. Given the size of my buttons, I can fit 3 per row in portrait, and 4 per row in landscape.
I reposition all of the buttons in: willRotateToInterfaceOrientation:duration and then call: setContentSize: on my UIScrollView. Everything seems to work just fine, with the exception of the auto-scrolling that occurs after the call to SetContentSize:. In other words, let's say I was in portrait, and scrolled 3/4 of the way down the list, when I rotate to landscape, it auto-scrolls all the back up to the top.
The interesting thing is, in the same scenario, if I were to do a small flick scroll up or down, and then immediately rotate the device, the scroll view redraws correctly, and retains the current scroll position!
So, to be clear, the culprit here seems to be SetContentSize:, but obviously I have to call that for the scroll view to scroll correctly.
Any ideas on how I can retain the current scroll position?
Thanks!

You might try implementing the UIScrollViewDelegate method scrollViewShouldScrollToTop:, which tells the caller whether to scroll to the top or not. You may have to have some flag in your delegate that indicates if a rotation is underway or not, because under normal conditions you may actually want the ability to tap the status bar and have the scroll view scroll to the top.
If, on the other hand, you don't ever want the scroll view to scroll to the top automaticlly, simply implement that delegate method and have it return NO.

I had this problem, just did this and it works well for me
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//calculate percentage down scrollview currently at
float c = self.scrollView.contentOffset.y / self.scrollView.contentSize.height;
//reposition subviews in the scrollview
[self positionThumbnails:toInterfaceOrientation];
//set the new scrollview offset to the same percentage,
// using the new scrollview height to calculate
self.scrollView.contentOffset =
CGPointMake(0, c * self.scrollView.contentSize.height);
}
In my case, I have a scrollview with a fixed width to device size, and variable height

Related

IOS UIScrollView: Cannot scroll until the end of the content

I am trying to generate a scroll controller in a window with the UIScrollView class, which will contain numerous UIButtons, placed vertically. I set the size of the scroll view equal to the current view controller's root view, so that the scroll view covers the entire visible window. Then I generate the UIButtons I am going to add to the scroll view: I add each UIButton just in the below of the previous UIButton and I add the height of the current UIButton to a variable called "totalContentHeight". Finally, I set the height of the contentSize of the scroll view to this value, in the following line of code:
self.scrollViewForNewsButtons.contentSize = CGSizeMake(self.view.frame.size.width, totalContentHeight);
totalContentHeight is equal to numOfButtons*eachButtonsHeight after I add all the buttons to the scroll view.
The problem is, in the simulator, when I run the app and scroll until the end of the last button and release the mouse, the last two buttons bounces back such that they lie outside of the visible window. It is somewhat hard to express with mere words, so here are the images:
1)This is what I get when I scrolled until the end of the content and held the content at the last possible position it could be pushed:
2)This is what I get after I released the mouse and the scroll view bounced back to its final position:
As you can see, the last two buttons are drawn outside of the visible area. It is like the scroll view's area covers the whole window plus the button area of the IPhone. I could not find a reasonable explanation for this. Am I setting the area size wrong or am I missing something else?
just set content size with calculation with your total button and its height...For Ex..
float yheight = totalButton * yourButtonHeight;
[yourScrollView setContentSize:CGSizeMake(320, yheight + 44)];
try this code...
And if you set the scrollview frame size the same as
self.view.bounds

iPhone how to implement a "Wide" UITableViewCell?

I'd like to have a UITableView with cells wider than 320 points. The user should be able to scroll sideways to be able to view different parts of a UITableViewCell. Is this kind of behavior possible with a UITableView, or should I go and try to implement a tiling UIScrollView?
I tried wrapping a UITableView within a UIScrollView, and the results are terrible - they compete for the scroll gestures and most of the time the scroll view wins, preventing the table from being traversed vertically.
Any input is appreciated!
Thank you!
Update: I tried the proposed solution and it scrolls properly, but the tableview is still only 320 pixels wide. Is tableView's width linked to the window bounds ?
Wrapping the table view with the scroll view is the right way.
UIScrollView with
Show horizontal scrollers
scrolling enabled
autosize to full screen
Inside that, a UITableView
shows vertical scrollers
scrolling enabled
Then I set the table view's frame, with w, being the calculated width of the table view with all columns, whatever your width, and kTableScrollViewHeight being the fixed height of both the table view and the scroll view, in my case, for example 367 points (screen minus status, navbar and tabbar):
tv.frame = CGRectMake(0, 0, w, kTableScrollViewHeight);
and the scroll view's content size
scrollView.contentSize = CGSizeMake(w, kTableScrollViewHeight);
If you want the scroll-to-top behavior when the user taps the status bar, you might want to set
scrollView.scrollsToTop = NO;
because otherwise the scroll view will take away the tap from the table view.

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

trouble with rotating view (and resizing elements within) in ipad application

I'm having a nightmare with the rotation on iPad. I've searched all over the place for some tutorials, but nothing seems to really be for what I want. (Possibly not searching for the right thing?!)
I have a portrait view by default which is an image and a button inside the view. When I rotate, I detect this can work out if it's landscape. I then try to set the frame size of the uiview to fit nicely on the screen.
If I let it autoresize, it simply stretches and fills the screen. This I don't want.
but the trouble is, when I resize, the button gets resized too, but not in the same ratio as the image.
My question is: What's the best way to resize the view. I wanted to simply reduce the uiview by say 60% and it resizes EVERYTHING in that view with the same 60%. The only way I see this is working at the moment is to create two views... but that's twice the work and maintenance!
I've tried messing with the autosizing arrows in Interface builder, but that again seems to screw things up more!
I'm completely lost here!! Thanks for any info
The problem you have there is that the view is automatically resized to the screen ratio. On an iPad in Portrait Orientation the screen size is 1024x768. After the rotation to Landscape the origin rotates too and your screen content is skewed or stretched to 768x1024.
What you need to do is to override the
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
message of the UIViewController of the view which rotates. This message is called within the animation block of the rotation. You just set the framesize of your subviews (the button) to whatever is best for you. Once i had a problem with rotating an OpenGL view. The content of the view was stretched when rotating to landscape. Since it is not possible to alter any OpenGL matrices within the animation block the only solution i found was to make the view quadratic and to set the origin behind the bounds of the screen (in -x direction). You have to override the message also to reset the origin above the screen (in -y direction) bounds in landscape mode, to keep the viewport in the middle of the screen. That way the view kept its ratio. Whatever solution is best for you, overriding this message should work out.
Have you tried disabling the autoresizesSubviews property on your UIView? It should prevent any size changes on the subviews when you resize your view.

How do i anchor subviews of a UIScrollView like the URL bar in mobile safari?

I want to do something similar to how mobile safari keeps its URL bar anchored at the top of the screen while a page is loading unless you scroll past the top, in which case it scrolls down with the rest of the content.
I, however, want the opposite to happen; I want specific subviews to scroll off the screen if I scroll down, but if I scroll to the top I want the subviews to stay anchored at the top of the screen while the rest of the content continues to scroll down. I suppose I could do some trickery with the view hierarchy where the subviews change their superview when we scroll to the top, but I'm wondering if there are any other more elegant solutions to this.
Very late answer to my own question, but I figured i'd reveal how I was able to do this.
I subclassed UIScrollView and implemented the layoutSubviews method, which is called every time the scroll position changes. Here, all you need to do is identify which view you want to anchor and do something like this:
- (void) layoutSubviews {
[super layoutSubviews];
CGFloat x = 0.0f;
CGFloat y = MIN(self.contentOffset.y,0.0f);
anchorview.frame = CGRectMake(x,y,anchorview.frame.size.width,anchorview.frame.size.height);
}
If you want the anchored view to stay at the top if you scroll past the top (instead of remaining in the same position relative to the scrollview), then this will do just that, since the content offset's y coordinate will be less than zero when you scroll beyond the very top of the view.
UITableView section headers already do this in plain tables, you could leverage that behavior.