scrolling view problem when device is changing orientation


/ Published in: iPhone
Save to your folder(s)

when changing orientation the scrollview has extra content size


Copy this code and paste it in your HTML
  1. #define SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT 800
  2. #define SCROLLVIEW_CONTENT_WIDTH_PORTRAIT 320
  3. #define SCROLLVIEW_CONTENT_HEIGHT_LANDSCAPE 670
  4. #define SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE 480
  5.  
  6. ........
  7. - (void)viewWillAppear:(BOOL)animated
  8. {
  9. [super viewWillAppear:animated];
  10.  
  11. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)
  12. name: UIKeyboardDidShowNotification object:nil];
  13. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:)
  14. name: UIKeyboardDidHideNotification object:nil];
  15.  
  16. m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT);
  17. displayKeyboard = NO;
  18. }
  19.  
  20. - (void)viewWillDisappear:(BOOL)animated
  21. {
  22. [[NSNotificationCenter defaultCenter] removeObserver:self];
  23. }
  24.  
  25. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  26. {
  27. // Return YES for supported orientations
  28. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  29. }
  30.  
  31. - (void)keyboardDidShow: (NSNotification *)notif
  32. {
  33. if (displayKeyboard) {
  34. return;
  35. }
  36.  
  37. if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
  38. self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
  39. {
  40. m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, 140);
  41. }
  42. else
  43. {
  44. m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, 240);
  45. }
  46.  
  47. CGRect textFieldRect = [m_activeField frame];
  48. textFieldRect.origin.y += 10;
  49. [m_scrollView scrollRectToVisible:textFieldRect animated:YES];
  50.  
  51. displayKeyboard = YES;
  52. }
  53.  
  54. - (void) keyboardDidHide: (NSNotification *)notif
  55. {
  56. if (!displayKeyboard) {
  57. return;
  58. }
  59.  
  60. if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
  61. self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
  62. {
  63. m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, 300);
  64. m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, SCROLLVIEW_CONTENT_HEIGHT_LANDSCAPE);
  65. }
  66. else
  67. {
  68. m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, 460);
  69. m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT);
  70. }
  71.  
  72. displayKeyboard = NO;
  73. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.