0
votes

I'm adding a UIScrollView to the reusable view I'm making and then adding that reusable view to a UIViewController.

In my reusable view I'm adding UIViews to the UIScrollView :

 int yLocation = 20;
    for (int i= 0 ; i < 5; i++) {
        UIView *tempView = [[offerCell alloc]initWithDelegate:self];
        tempView.frame = CGRectMake(5, yLocation, 290.0F, 70.0F);

        yLocation += 90;

        [self.scrollView addSubview:tempView];
        UILabel *tempLabel = [tempView viewWithTag:1000];
        tempLabel.text = [NSString stringWithFormat:@"Label Number %d" , i];
        [tempLabel sizeThatFits:CGSizeMake(1000, 1000)];
    } 
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, yLocation);

Then I'm adding the UIView to my UIViewController

UIView *tempView = [[BuyTokensView alloc] initWithDelegate:self andViewController:self];
[self.view addSubview:tempView];
CGPoint point = CGPointMake(10, 0);
tempView.center = point;

The view is shown correctly but I can't scroll the UIScrollView. My UIScrollView height is smaller than the Height I'm assigning in ContentSize, so why can't I scroll?

Here is a picture of the UIViewController: image 1

2
log scrollview height. Check whether you add scrollview as subview. - Aravind Bhuvanendran
I add the scrollview as a subview I wrote that, and the height is fine. - Eli Braginskiy
i think you should add tempview over scrollview [self.scrollView addSubView:tempView] - Aravind Bhuvanendran
@AravindBG what do you mean? I add tempView to scrollView.. [self.scrollView addSubview:tempView]; - Eli Braginskiy
i am telling about the second code snippet. - Aravind Bhuvanendran

2 Answers

1
votes

Either you can try to disable autolayout or use the strategies in this technical note:

https://developer.apple.com/library/ios/technotes/tn2154/_index.html#//apple_ref/doc/uid/DTS40013309

0
votes

I'm using Xcode 6/iOS 8+ now with auto-layout turned on, and I had the same problem in the first place, removing auto-layout doesn't work at all, so in order to make scroll view scrollable vertically, I made sure the following:

  1. The scroll view content size height MUST be bigger than the screen height, this almost goes without saying...
  2. The top/bottom/left/right constraint of the scroll view HAS TO BE pinned, I did this using storyboard so no code showing up here

If you want to make the scroll view scrollable horizontally, then make sure its content size width is bigger than the screen width, the other rule applies the same.

This worked for me, hope it can help someone.