8

I'm using autolayout to change the height of my scrollview. I add some subviews to the scrollview where I manually increment the content size. But when I get an event where I am about to change the height the content size is zero.

Here is the code:

-(void) addTag:(NSString *)tagName {

    ...
    [button addTarget:self action:@selector(tagPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self setContentSize:CGSizeMake(self.frame.size.width, self.contentSize.height+60)];
    [self.items addObject: button];
    NSLog(@"content height: %0.2f", self.contentSize.height);


}

Console Output:

 2013-04-18 18:23:06.106 TouchSelectApp[19944:c07] content height: 120.00

Here I output the contentSize :

- (void)tagPressed:(UIButton *)sender {

    NSLog(@"1 content height: %0.2f", self.contentSize.height);
    ...
}

The console shows:

 2013-04-18 18:17:24.747 TouchSelectApp[19944:c07] 1 content height: 0.00

I don't why the content height is set to zero. Help Me!

Update: Here is the problem I am trying to solve.

I am have a scroll view with buttons. Initially it only shows one button. When the button is pressed tagPressed: is called. Tag Pressed changes the height to show all the buttons. Then when the user selects a button the scrollview's height is changed to the original and the offset is set to the button's origin. The below code works but the animation is all wrong. If I change the offset and before the animation block then it just sets the offset to zero.

- (void)tagPressed:(UIButton *)sender {

     CGPoint p;
     float maxHeight = self.items.count * 60;


     if (self.height.constant == maxHeight) {
        self.height.constant = 60;
        p = CGPointMake(0, sender.frame.origin.y-5);     
     }else {
         self.height.constant = maxHeight;
         p = CGPointMake(0, 0);
     }

     [self.superview setNeedsUpdateConstraints];

     [UIView animateWithDuration:0.5f animations: ^{
          [self.superview layoutIfNeeded];
     } completion:^(BOOL finished) {
          [self setContentOffset:p];

     }];

}

Venkat S. Rao
  • 1,110
  • 3
  • 13
  • 29
  • I will update the post with the original problem. – Venkat S. Rao Apr 19 '13 at 15:18
  • I do not see why you need a scroll view for this at all. You have a view that either shows one button or shows all the buttons; where's the scrolling? Just use a view that changes size. Even better, just show and hide and the buttons as needed, perhaps. – matt Apr 19 '13 at 16:13
  • Note that a scroll view is so the *user* can scroll. *You* can scroll (i.e. show one particular button) without a scroll view, so if that's all you're trying to do, I would suggest avoiding the scroll view, which adds a lot of complications you don't need (as far as I can tell). – matt Apr 19 '13 at 16:15
  • @matt thanks for the response. At the moment the user does not need to scroll. After I get this functionality working, I will be adding many more buttons that will not fit height of the iPhone and then the user will need to scroll. – Venkat S. Rao Apr 19 '13 at 16:49
  • I was afraid you might say that. :) Well, you're going to have to the `contentSize` working before you can set the `contentOffset`, because if the size isn't bigger than the scroll view there is nothing to scroll! So either do not use autolayout or use one of the two ways of configuring a scroll view with autolayout described in the link you've already been given (or in my "duplicate" link above). – matt Apr 19 '13 at 17:22
  • Also, here's the section of my book that might help you: http://www.apeth.com/iOSBook/ch20.html#_creating_a_scroll_view – matt Apr 19 '13 at 17:24

1 Answers1

12

If you use auto layout with a UIScrollView, you don't set the contentSize property. See here for more information (scroll down to the section on UIScrollView).

fumoboy007
  • 5,345
  • 4
  • 32
  • 49
  • 2
    I'm really trying to change the content offset. How can I do that? – Venkat S. Rao Apr 18 '13 at 22:44
  • 2
    You can set the ContentSize in the ViewDidLayoutSubviews. That works out fine for me using Autolayout i.e. -(void)viewDidLayoutSubviews { [_scrollView setContentSize:CGSizeMake(_scrollView.width * [_imageViews count], 0)]; } – Yasper Jun 09 '15 at 15:18
  • Just for someone searching for quick answer. In general you should set constraints of all you items so that they constrain to all edges of contentView of your scrollView. – Ivan Tkachenko Jan 20 '22 at 15:47