Grace
Grace

Reputation: 1265

Objective-C UIScrollView constraints

I have an UIScrollview which will contain X views, each view is of Class type "CategoryPostView" that contains an UIImageView. I am adding these views on the scrollView one after another. But when the image loads in the imageView I want to change the size of my imageView, therefor I want to change the Offset y of my all other views in the scrollView. I am using autolayout constraint on UIScrollView but I couldn't reach to any result. This is the code I am using:

    int categoryScrollY = 0.0;
        CategoryPostView *previousView = nil;
            for(; self.indexOfCate

goryInTheScroll < [self.categoryItemsArray count]; self.indexOfCategoryInTheScroll++){

            PostItem *postItemObj = [self.categoryItemsArray objectAtIndex:self.indexOfCategoryInTheScroll];
            CategoryPostView *categoryPostViewObj = [[CategoryPostView alloc] initWithFrame:CGRectMake(0.0, categoryScrollY, 148.0, 76.0)];
            categoryPostViewObj.translatesAutoresizingMaskIntoConstraints = NO;  //This part hung me up

            [categoryPostViewObj setupPostViewWithItem:postItemObj];
            [self.categoriesScrollView addSubview:categoryPostViewObj];


            categoryScrollY += 80.0;


            [_categoriesScrollView addConstraints:
             [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[categoryPostViewObj]"
                                                     options:0 metrics:nil
                                                       views:@{@"categoryPostViewObj":categoryPostViewObj}]];

            if (!previousView) { // first one, pin to top
                [self.categoriesScrollView addConstraints:
                 [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[categoryPostViewObj]"
                                                         options:0 metrics:nil
                                                           views:@{@"categoryPostViewObj":categoryPostViewObj}]];
            }else{
                [self.categoriesScrollView addConstraints:
                 [NSLayoutConstraint
                  constraintsWithVisualFormat:@"V:[previousView][categoryPostViewObj]"
                  options:0 metrics:nil
                  views:@{@"categoryPostViewObj":categoryPostViewObj, @"previousView":previousView}]];
            }

            if(self.indexOfCategoryInTheScroll == ([self.categoryItemsArray count] - 1))
            {

                [self.categoriesScrollView addConstraint:[NSLayoutConstraint constraintWithItem:categoryPostViewObj
                                                                       attribute:NSLayoutAttributeRight
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:self.categoriesScrollView
                                                                       attribute:NSLayoutAttributeRight
                                                                      multiplier:1.0
                                                                        constant:0]];
            }

            previousView = categoryPostViewObj;

        }

For now, I have the views on top of each other. I have to somehow apply my constraint from the bottom of the previous view and not from the top of the previous view. I guess I should add options to this line of code:

[self.categoriesScrollView addConstraints:
                     [NSLayoutConstraint
                      constraintsWithVisualFormat:@"V:[previousView][categoryPostViewObj]"
                      options:0 metrics:nil
                      views:@{@"categoryPostViewObj":categoryPostViewObj, @"previousView":previousView}]];

Many thanks!

Upvotes: 3

Views: 276

Answers (2)

Grace
Grace

Reputation: 1265

I ended up using the class UIView+AutoLayout. You can find it here https://github.com/smileyborg/UIView-AutoLayout and https://github.com/jrturton/UIView-Autolayout

Upvotes: 0

Fogmeister
Fogmeister

Reputation: 77621

The best way to use constraints inside a UIScrollView is to not use them at all.

If you want to layout the inside of the scroll view using constraints then create a UIView to contain everything in the scroll view and then place this view into the scroll view.

By doing this you can now use the container view that you created to place all the constraints inside.

Upvotes: 3

Related Questions