Return to Snippet

Revision: 27144
at May 25, 2010 03:00 by devb0yax


Initial Code
#define SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT	800
#define SCROLLVIEW_CONTENT_WIDTH_PORTRAIT	320
#define SCROLLVIEW_CONTENT_HEIGHT_LANDSCAPE     670
#define SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE	480

........
- (void)viewWillAppear:(BOOL)animated 
{
	[super viewWillAppear:animated];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) 
										name: UIKeyboardDidShowNotification object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) 
										name: UIKeyboardDidHideNotification object:nil];
	
	m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT);	
	displayKeyboard = NO;
}

- (void)viewWillDisappear:(BOOL)animated 
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
	// Return YES for supported orientations 
	return (interfaceOrientation !=	UIInterfaceOrientationPortraitUpsideDown);
}

- (void)keyboardDidShow: (NSNotification *)notif 
{
	if (displayKeyboard) {
		return;
	}	
	
	if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
		self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
	{
		m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, 140);
	}
	else 
	{
		m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, 240);
	}
	
	CGRect textFieldRect = [m_activeField frame];
	textFieldRect.origin.y += 10;
	[m_scrollView scrollRectToVisible:textFieldRect animated:YES];

	displayKeyboard = YES;
}

- (void) keyboardDidHide: (NSNotification *)notif 
{
	if (!displayKeyboard) {
		return; 
	}
	
	if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
		self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
	{
		m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, 300);
		m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, SCROLLVIEW_CONTENT_HEIGHT_LANDSCAPE);
	}
	else
	{
		m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, 460);
		m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT);
	}
	
	displayKeyboard = NO;	
}

Initial URL


Initial Description
when changing orientation the scrollview has extra content size

Initial Title
scrolling view problem when device is changing orientation

Initial Tags


Initial Language
iPhone