2

I have an issue where i am adding a UIScrolview to my UIViewController, but my UIScrollview doesnt want to scroll. I want it to work the same way that it works on a UITableViewController.

Here i initialize my UIScrollview

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
scroll.contentSize = CGSizeMake(320, 800);
scroll.backgroundColor = [UIColor redColor];
scroll.showsHorizontalScrollIndicator = YES;
scroll.scrollEnabled = YES;
scroll.userInteractionEnabled=YES;
[self.view addSubview:scroll];

Here i add my content to the scrollview:

UILabel *memberNo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
memberNo.text = @"Member No";
memberNo.font = [UIFont boldSystemFontOfSize:16];
memberNo.textColor = [UIColor limeColor];
[scroll addSubview:memberNo];

memberText = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 300, 30)];
memberText.placeholder = @"Member No";
memberText.borderStyle = UITextBorderStyleRoundedRect;
memberText.text = @"1111111";
[scroll addSubview:memberText];
Abizern
  • 146,289
  • 39
  • 203
  • 257

5 Answers5

2

Try this

set content size more than the screen size and it will scroll whether there is more content or not

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];

HorizontalScroll

 scroll.contentSize = CGSizeMake(320, 900);

VerticalScroll

scroll.contentSize = CGSizeMake(340, 800);
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82
1

As others have said, the scroll view's size (specifically, its bounds.size) must be smaller than its contentSize for it to actually scroll.

You probably want your scroll view to fit inside its superview, so try setting its frame to the superview's bounds:

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

The content is the same size as the scrollview: there is nothing to scroll.

The scrollview needs to contain a view that is bigger than it's own size.

Abizern
  • 146,289
  • 39
  • 203
  • 257
0

Try setting the contentSize to be larger than the frame. For example:

scroll.contentSize = CGSizeMake(1000, 800);

If that doesn't work, then check where you are setting the content size. Where are you initializing the UIScrollView? Try moving the above line to viewWillAppear or viewDidLayoutSubviews.

Steph Sharp
  • 11,462
  • 5
  • 44
  • 81
0

frame of the scroll view refers to the size of the scroll view itself whereas content size is the size for the view which is to be shown in the scroll view. If the size of view to be shown in scroll view is having smaller size than the scroll views frame size, then it won't at all scroll, but if you set content size more then scroll view has to scroll to show the 'extra' size more than its own frame size. e.g. If height of scroll view is 100 and you have set the height factor in content size equal to 150, then these extra 50 pixels will be scrollable.

Good Luck!

Yogi
  • 3,578
  • 3
  • 35
  • 56