scrolling - iOS: Autolayout causing UIScrollView to not scroll




uiscrollview not scrolling swift autolayout (5)

viewDidLoad is indeed too soon, but viewDidAppear:animated might not be late enough either. The ideal place to set the contentOffset is inside viewDidLayoutSubviews.

I have set up a UIScrollView with which I want to display 12 images (only 8 fit on screen) laid out horizontally. In the following image you can see the problem I'm having (which makes my scroll view not scroll), my constraints and the UIScrollView which I have added on storyboard:

I have called the following method on -(void)viewDidLoad, where I "set up"my scrollview (itemList is my scroll view property and itemNames a array with the images'names):

- (void)setupHorizontalScrollView
{
    self.itemList.delegate = self;
    [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.itemList setBackgroundColor:[UIColor blackColor]];
    [self.itemList setCanCancelContentTouches:NO];

    self.itemList.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    self.itemList.clipsToBounds = NO;
    self.itemList.scrollEnabled = YES;
    self.itemList.pagingEnabled = NO;

    NSInteger tot=0;
    CGFloat cx = 0;
    for (; ; tot++) {
        if (tot==12) {
            break;
        }

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.itemNames objectAtIndex:tot]]];

        CGRect rect = imageView.frame;
        rect.size.height = 40;
        rect.size.width = 40;
        rect.origin.x = cx;
        rect.origin.y = 0;

        imageView.frame = rect;
        [self.itemList addSubview:imageView];
        cx += imageView.frame.size.width;
    }

    [self.itemList setContentSize:CGSizeMake(cx, [self.itemList bounds].size.height)];
}

I have added the [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO]; because I saw this suggestion on other posts, but it doesn't work with or without it. The only way it works is if I uncheck use AutoLayout on the storyboard, but that moves the UIImageViewI use to look as a navigation bar to the bottom of the screen. I don't know what to do anymore, any help is appreciated :)


Insert: [scrollView setContentSize:CGSizeMake(x,y)]; in the following method:

-(void)viewWillAppear:(BOOL)animated

Try to set your scrollView's Content size int "viewDidLayoutSubviews" method with keeping the autolayouts set.

-(void)viewDidLayoutSubviews
{
  [self.itemList setContentSize:CGSizeMake(required_width, required_height)];
}

Two Solutions:

  1. Create different constraints that can be satisfied simultaneously (you will have to edit). I think the problem is your bottom space and top space constraints are mutually exclusive. please remove one and try again. IF this is difficult for you, try adding another UIView to contain the UIScrollView to help manage your constraints, it might seem odd at first, but sometimes adding another view to contain your view actually makes it simpler at each level.

  2. Turn off Autolayout, and change the autoresize masks of your UIImageView to be what you wish.


iOS horizontal UIScrollbar with Autolayout

You have to add constraints programmatically if you want to achieve scrolling. And each side of your scroll view has to have at lest one constraint tied to it. So for example if you have 2 images you would have 3 horizontal constraints: |-firstImage-secondImage-| and 2 vertical constraints for every image: V:|-fistImage-| and V:|-secondImage-|. This way you can set the content size of your scroll view. You can find the Apple's guide here.