1
votes

I had this structure on my storyboard (i have added some example name to easly understand):

  • UIView (the main view) "BlackView"

    -UIScrollView (inside the main view) "OrangeView"

    • UIView "YelloView" (ContentView inside main scroll)
      • UITableView "BluView" (Inside and on the top of the contentView)
      • UIView "RedView" (Inside of the ContentView and on the same level of UITableview)

What i wanna achive? I want a table that grow proportionally to his rows number (the row height is fixed to 60). I also want an UIScrollview that grow in content_size and scroll ability accordingly to the heights sum of the BlueView and the RedView.

The autolayout constraints are:

UiScrollView "OrangeView": (top 0,trailing 0,bottom 0,leading 0)

UIView "YelloView": (top 0,trailing 0,bottom 0,leading 0) (equal width to main ed equal height to main "BlackView" , priority “low” for bottom)

UITabelView "BluView": (top 0,trailing 0,leading 0)

UIView "RedView": (Vertical space 2 to "BluView",trailing 0,leading 0,bottom 0 to YelloView)

I have a complete chain of constraints from top to bottom of the view. UITableView is free to grow because i haven't set directly his "height" constraint and uitableview setscrollingenabled is FALSE. The result is a tableview that don't grow.

I can produce the growing by code:

Table.frame=CGRectMake(BluView.frame.origin.x, BluView.frame.origin.y, BluView.frame.size.width, BluView.contentSize.height);

I works, the table frame and the "OrangeView" scroll content_size grow correctly but "RedView" doesn't respond to this frame change and it doesn't move to the new "table bottom" ad 2 of distance.

What i have already tried to refresh the constraints system:

    [BlackView layoutIfNeeded];
    [BlackView setNeedsUpdateConstraints];
    [RedView layoutIfNeeded];
    [ScrollContainer layoutIfNeeded];
    [RedView layoutIfNeeded];
    [RedView setNeedsUpdateConstraints];
    [OrangeView setNeedsUpdateConstraints];
    [RedView updateConstraints];
    [OrangeView updateConstraints];
    [BlackView updateConstraints];
    [BluView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [RedView setTranslatesAutoresizingMaskIntoConstraints:NO];

I have called those methods before and after the uitable frame change, they doesn't refresh the RedView's y position. How i can obtain the desired behaviour? There's a way to create anothere type of "costraints tree" or maybe there's another way to refresh the position of the RedView after i have programmatically change the Tableview frame?

1

1 Answers

1
votes

UITableView is free to grow because i haven't set directly his "height" constraint and uitableview setscrollingenabled is FALSE.

Setting setscrollingenabled to false doesn't tell the tableView to have the height of its content, it just disables the scroll behavior. So if your tableView is 200pt tall and you have a content of 500pt, you'll never see the 300pt at the bottom.

Add a height constraint to your tableView and update its constant with the tableView's contentSize.height every time the contentSize changes. This way it will always have the size of its content, and because it's in a scrollView you'll still be able to scroll to see all the content.

PS: The yellow view is useless here if it's only a container for the blue and red views.