iPhone question: UIScrollView / scrollRectToVisible not scrolling

2,411 views
Skip to first unread message

Brad O'Hearne

unread,
Dec 15, 2008, 1:48:25 PM12/15/08
to xcodep...@googlegroups.com
I have a simple UIScrollView with one text field I am trying to get to scroll when the keyboard appears. I have been trying to run code which is almost identical to that in the iPhone Application Developer's guide to accomplish this. The code compiles cleanly, runs cleanly, but absolutely nothing happens -- there is no scrolling whatsoever. Here is the code: 

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
if (keyboardShown)
return;
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Resize the scroll view (which is the root view of the window)
CGRect viewFrame = [self.view frame];
viewFrame.size.height -= keyboardSize.height;
self.view.frame = viewFrame;
// Scroll the active text field into view.
CGRect textFieldRect = [searchStringTextField frame];
UIScrollView *scrollView = (UIScrollView *)self.view;
[scrollView scrollRectToVisible:textFieldRect animated:YES];
keyboardShown = YES;
}



I've run this thing through the debugger, and all variables seem to populated properly, but absolutely nothing happens to the view that needs to be scrolled. Any ideas what might be the problem here? 

Thanks,

Brad

JoeSoftwareOps

unread,
Dec 15, 2008, 3:10:45 PM12/15/08
to xcodep...@googlegroups.com
Brad,

First if you have your view hierarchy setup properly, and if you have the scroll view contentSize set to the proper size and you use becomeFirstResponder as in [yourUITextField becomeFirstResponder]  it will scroll automatically.  The key is setting the contentSize of the UIScrollView

This code snipet can get you moving in the right direction.

I have the following view hierarchy,

ScrollView that contains a contentView that contains the editField (Now shown)

The contentView is just a UIView that holds all the other edit views.  For the scrollView to scroll its contentSize is set from contentView.frameRect,size which for it to actually scroll has to be larger than 460.

I know this isn't a full example, but the contentSize is the key to scrolling.

-(void)viewDidLoad
{
CGRect theScrollRect;
theScrollRect = ((UIScrollView*)self.view).frame;
theScrollRect.size.height460;
((UIScrollView*)self.view).frame = theScrollRect;
((UIScrollView*)self.view).contentSize = contentView.bounds.size;
Sincerely,

Joe Michels

Software Ops LLC


Bradley S. O'Hearne

unread,
Dec 15, 2008, 3:17:37 PM12/15/08
to xcodep...@googlegroups.com
Joe,

You are right as rain. The thing which tripped me up is that I created this UIScrollView in IB, and figured it would set the content size based upon the view size set in IB, but it doesn't touch the content size. Weird that you can't set the content size in IB (or at least I am unaware of it). 

I did have to do some extra contortions in some of the keyboard notifications to make the scrolling smooth as I wanted it, but it works now. 

Thanks for your input. 

Cheers,

Brad

JoeSoftwareOps

unread,
Dec 15, 2008, 3:46:23 PM12/15/08
to xcodep...@googlegroups.com
Glad I could help.


Sincerely,

Joe Michels

Software Ops LLC



Reply all
Reply to author
Forward
0 new messages