2

I have created an app for iPhone and it is perfectly positioned to fit for iPhone 5/5S/5C screens. However, I am trying to use a ScrollView for when it is used on an iPhone 4/4S (smaller screen).

My view is embedded in a Navigation Bar Controller and also is in a Tab Bar Controller. My Scroll View is in the space between the two and when it is in the iPhone 5/5S/5C I don't want it to scroll, as it is perfect size already. This part works fine.

However, when I test it on the smaller screen size on the simulator I want it to be able to scroll, but it isnt scrolling at all.

I have an IBOutlet for my scrollview and I use the following lines of code in my .m file to setup the scroll view.

if (self.view.bounds.size.height == 568)
{
    [_Scroller setContentSize:CGSizeMake(320, 1500)];
    _Scroller.frame = CGRectMake(0, 64, 320, self.view.bounds.size.height);
}
else
{
    [_Scroller setContentSize:CGSizeMake(320, 80)];
    _Scroller.frame = CGRectMake(0, 64, 320, self.view.bounds.size.height);
}

_Scroller.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
[_Scroller setScrollEnabled:YES];

I also have Adjust Scroll View Insets in my view controller unticked, as it kept adding an annoying gap at the top of the scroll view.

Can anyone help me with where I am going wrong? It is driving me mad! Any help would be great. Also I am fairly new to iOS Development.

Thanks Guys!

EDIT:

I've changed the if statement to this but it still doesn't work:

if (self.view.bounds.size.height == 568)
{
    [_Scroller setContentSize:CGSizeMake(320, 1500)];
    _Scroller.frame = CGRectMake(-4, 64, 329, self.view.bounds.size.height);
}
else
{
    [_Scroller setContentSize:CGSizeMake(320, 2000)];
    _Scroller.frame = CGRectMake(-4, 64, 329, self.view.bounds.size.height);
}

_Scroller.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
[_Scroller setScrollEnabled:YES];
Stephen Bennett
  • 431
  • 6
  • 17
  • i think your if condition is wrong check your else part in which your contentsize is too small so make it larger so you can scroll. your frame is ok but contentsize is too small. check that. – i-Maddy Apr 15 '14 at 09:59
  • use delta if necessary http://stackoverflow.com/a/18744952/1106035 – Paresh Navadiya Apr 15 '14 at 10:00
  • I've changed the code but its still not working. I dont understand what I'm doing wrong. @Prince I've had a look at that link but am a bit confused. – Stephen Bennett Apr 15 '14 at 10:09

1 Answers1

4

Use like this:

if ([[UIScreen mainScreen] bounds].size.height >= 568) //iphone 5/5c/5s/6/6 plus
{
  //making ContentSize and frame's height same as not need of scrolling
  //make changes in height if necessary
  [_Scroller setContentSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height-64)]; 
  //make changes in height if necessary
  _Scroller.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64); 
}
else //iphone 4/4s
{
   //making ContentSize greater than frame's height as we need scrolling
   //make changes in height if necessary
   [_Scroller setContentSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height+64)]; 
   if([[UIDevice currentDevice].systemVersion hasPrefix:@"7"]) //iOS 7.0 >
   {
     //made changes in y as status bar height is counted
     _Scroller.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64);
   }
   else //iOS 6.1 <
   {
      //made changes in y as status bar height not counted
      _Scroller.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height-64);
   }
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132