0

I have a UIScrollView that has an outlet and in my implementation file, I've even programmatically added the objects that I need on there. When I run my program, the NSLog states that it's up but on the simulator, it's showing nothing. There's not even any scrolling action visible.

I've done it before and double checked with the other view controller and everything seems to line up but it's still not wanting to show. I declared the IBOutlet in .h and in .m I've set a NSDictionary in the array's method that contains the objects I need on the scrollview.

I then tell it to add it to the subview, [self.scrollview addSubview:someView]; where someview is the main object that has other objects laying on top. This has worked before but for some reason, is failing to work at this time. I've also ensured I added the method for the array to the viewDidLoad method. I've connected the outlet in Storyboard but get no visual result. I'm using lorempixel to populate the images and as stated before, it's working on another VC but not on this one. It's probably something simple I'm missing. Thanks for the help.

-(void)addItemsToThis:(NSMutableArray *)anArray {

int i = 0;
float contentHeight = firstRowGutterY;

for (NSDictionary *item in checkout) {
    int row = (int) i / 3;
    float xOffset = i % 3 == 0 ? firstColumnGutterX : (i % 3) * itemContainerWidth + (xGutter * (i % 3) + firstColumnGutterX);
    float yOffset = i < 3 ? firstRowGutterY : firstRowGutterY + (row * itemContainerHeight) + (row * yGutter);

    UIView *itemCont = [[UIView alloc] initWithFrame:CGRectMake(xOffset, yOffset, itemContainerWidth, itemContainerHeight)];
    [itemCont setBackgroundColor:[UIColor clearColor]];

    UIView *whiteBox = [[UIView alloc] initWithFrame:CGRectMake(0, 6, 88, 112)];
    [whiteBox setBackgroundColor:[UIColor whiteColor]];

    UIButton *delete = [[UIButton alloc]initWithFrame:CGRectMake(70, 2, 24, 26)];


    UIImage *main = [UIImage imageWithData:[NSData dataWithContentsOfURL:[item objectForKey:@"image"]]];
    UIButton *imageBtn = [[UIButton alloc]initWithFrame:CGRectMake(6, 6, 78, 78)];
    [imageBtn setBackgroundImage:main forState:UIControlStateNormal];

    [whiteBox addSubview:imageBtn];
    [itemCont addSubview:whiteBox];
    [itemCont addSubview:delete];

    [self.scrollview addSubview:itemCont];

    i++;

    if (i % 3 == 0) {
        contentHeight += itemContainerHeight + yGutter;
    }
    NSLog(@"%f", contentHeight);
}
self.scrollview.contentSize = CGSizeMake(self.scrollview.contentSize.width, contentHeight);

}

TomG103
  • 311
  • 2
  • 26
  • When you set background color to different one,let's say red, do you see your scroll view on the screen ? – limon May 27 '14 at 15:23
  • I changed the background color to red and it is not showing up. I checked the connections against the ones that work and they all match up. The code looks the same minus the variable names too, not sure what I'm missing lol. – TomG103 May 27 '14 at 15:26
  • Are you using Autolayout ? – limon May 27 '14 at 15:27
  • I am using Autolayout – TomG103 May 27 '14 at 15:33
  • It was something simple...hierarchy can get you when you least expect it. I was calling all the items to the view after the scrollview instead of after. Thanks for the help all. – TomG103 May 27 '14 at 17:43

1 Answers1

0

Just check if you have not missed any of the following steps -

  1. Storyboard - Disable autolayout. The relationship between UIScrollView and auto layout is different from other aspects of auto layout. Refer to this thread for a solution -

    enter image description here

  2. On View Controller - Choose 'Freeform'

    enter image description here

  3. In viewDidLoad, Assumed that you set the same height in your View and Scroll View of Storyboard.

     [myScroll setScrollEnabled:YES];
     [myScroll setContentSize:CGSizeMake(320, 3000)];
  4. If nothing works, try to implement a simple Scroll View from scratch. You will figure out the problem eventually.

Community
  • 1
  • 1
raurora
  • 3,623
  • 1
  • 22
  • 29
  • I have so many segues and UIs already created that when I disable AL, it goes haywire on the look. I have set most of the UIs to freeform as they have to conform to a container and the other scrollviews are working perfectly with 3.5 and 4. For some reason, this one is just being difficult. It almost feels as though Apple said I can only use 5 Scrollviews and not 6 lol. I will add the setScrollEnabled though, maybe it will help. – TomG103 May 27 '14 at 15:41
  • Try these 2 steps - 1) UIView: Position with constraints. 2) UIScrollView: Bind to UIView with constraints. I found this [blogpost](http://codehappily.wordpress.com/2013/09/26/ios-how-to-use-uiscrollview-with-auto-layout-pure-auto-layout/) helpful. – raurora May 27 '14 at 15:46
  • Set constraints to 0 and still not showing. I've connected the outlets and everything as well. I'm not sure what you mean by compression resistance though. – TomG103 May 27 '14 at 15:50
  • @TomG103 I found this guy, he did not disable `AutoLayout` but still solved the solution - his [video](https://www.youtube.com/watch?v=4oCWxHLBQ-A). – raurora May 27 '14 at 15:56
  • The issue I think that will cause some hiccups, is that the Scrollview has boundaries within the UI. So instead of dragging it below the storyboard view, it has to set in a certain area. It's worked on a few other UIs but for some reason, it's not working now. I did try the constraints and could see the images loading behind the wall, just have to get it on top instead of behind. – TomG103 May 27 '14 at 16:17