0

I'm currently working on a iPhone 5 app that has two buttons in the left and right bottom corners of the 4-inch screen and I want to use a scroll to reach them when using iPhone 4 (3.5-inch). I've put the ScrollView on the xib and put the buttons beneath. My .h file is like that:

@interface learnView : UIViewController {
    __weak IBOutlet UIScrollView *scroll;
}
- (IBAction)doneLearn:(id)sender;
- (IBAction)randomLearn:(id)sender;

The scroll outlet is linked in the file's owner and I've enabled the scroll in the .m file:

[super viewDidLoad]
[scroll setScrollEnabled:YES];
[scroll setContentSize:CGSizeMake(320, 503)];

I'm not sure about if the CGSizeMake above is defined like it should but the values represent the size of my scrollView on the xib. Also I've set the size of the view as "Freeform". Doing all that has no effect when I run the simulator, the scroll not being present in the view. What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Daniela costina Vaduva
  • 1,857
  • 5
  • 20
  • 22

3 Answers3

1

To scroll scroll view you must set content size bigger than frame

Just like

[super viewDidLoad]
[scroll setScrollEnabled:YES];
[scroll setFrame:self.view.bounds];
[scroll setContentSize:CGSizeMake(320, 603)];

Try this, it will definitely help you....

Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
1

I think in your case frame height is the problem. In 3 inch device the frame got extended. try this

 [super viewDidLoad]
 [scroll setScrollEnabled:YES];
 [scroll setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
 [scroll setContentSize:CGSizeMake(320, 603)];

set the frame to the height of device as above. make sure this scrollview is added in the view. And also print and check

  NSLog(@"Scroll view height %@ App frame height %@",scroll.frame.size.height,self.view.frame.size.height);

EDITED: Add scrollview and buttons from the code.

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, 320, 460)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 50)];
[scrollView addSubview:button];
[scrollView setContentSize:CGSizeMake(320, 568)]; 
[self.view addSubview:scrollView];
Cintu
  • 913
  • 2
  • 16
  • 32
  • My scrollView is situated in the view at X = 0 & Y = 65 coords. How this affect the above code? I should use something like `... CGRectMake(0, 65, ...` ? – Daniela costina Vaduva Oct 08 '13 at 16:35
  • @DanielacostinaVaduva - If scrollview height is more than the device frame height then scrollview wont scroll. So just print this and confirm the heights "NSLog(@"Scroll view height %@ View Height - %@",scroll.frame.size.height, self.view.frame.size.height);" – Cintu Oct 08 '13 at 16:38
  • Is the issue in iphone 3 inch device? For 3 inch device height should be 480 not 568.. And tell me what content height u have added? – Cintu Oct 08 '13 at 16:53
  • The issue is for the 3.5-inch device which I run on the simulator and I've used the code above. Some print-screens would help? – Daniela costina Vaduva Oct 08 '13 at 16:55
  • In 3.5 inch device self.view.frame.size.height value should be 480.00. If it is 568.0 then you are running 4 inch rather than 3.5 inch. Please check. – Cintu Oct 08 '13 at 16:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38807/discussion-between-cintu-and-daniela-costina-vaduva) – Cintu Oct 08 '13 at 16:59
  • it also wont scroll unless the set contentSize is larger then the actual rect – John Riselvato Oct 09 '13 at 18:10
1

The issue is because even though you launch in 3.5" simulator xib size is still the size of the 4" screen (320,568)

You can try manually loading the scroll view using this.

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(210, 450, 100, 100)];

[button setTitle:@"button" forState:UIControlStateNormal];

[button addTarget:self action:@selector(handleButtonTap:) forControlEvents:UIControlEventTouchUpInside];

button.backgroundColor = [UIColor blueColor];

[view addSubview:button];

view.backgroundColor = [UIColor blackColor];

[scrollView addSubview:view];
[scrollView setContentSize:CGSizeMake(320, 568)];

[self.view addSubview:scrollView];
ManicMonkOnMac
  • 1,476
  • 13
  • 21