0
votes

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?

3
can you add some screenshot with the question ? the buttons are inside the scrollview or are below the scrollview ?Shubhank
below meaning inside, I am sorry for my way of explaining..Daniela costina Vaduva
can you set a background color of scrollView and see if the scrollview is showing with that color?Shubhank
I've changed with grey and it is shownDaniela costina Vaduva
My answer [here][1] may be helpful. [1]: stackoverflow.com/questions/18751742/…Jim Holland

3 Answers

1
votes

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];
1
votes

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....

1
votes

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];