What conditions can prevent layoutSubviews from being called after setNeedsLayout?

Problem

In the user view my (a UIScrollViewsubclass), I call setNeedsLayoutin response to the "reload data" event (triggered by an external source). In most cases, this works correctly, and layoutSubviewsis called when the next view layout loop occurs. Sometimes, however, layoutSubviewsit is not called! Until now, I have lived with "certain knowledge," which setNeedsLayoutalways evokes layoutSubviews. Apparently, I was wrong. I even tried to call layoutIfNeededafter setNeedsLayout, but still did not have time.

Question

Obviously, I would like to solve my specific problem. On the other hand, I would like to improve my understanding of the presentation layout process in iOS, so I formulate my question in a general way: do you know about any conditions that may interfere with layoutSubviewsbeing called after it has setNeedsLayoutbeen called? Answers that focus on UIScrollVieware highly welcome, since that is where I have problems.

Problem context

I'm on iOS 7.1 using Xcode 5.1.1. Notes on implementing my custom scroll view:

  • In the scroll view, there is one kind of container type UIViewthat always matches the size of the scroll content
  • The scroll implementation layoutSubviewsputs custom subtasks in the container view, manually calculating subviews frames

reloadData:

- (void) reloadData
{
  // Iterates through an internal array that holds the subviews,
  // then empties the array. Subviews are deallocated at this
  // point.
  [self removeAllSubviewsFromContainerview];
  self.contentOffset = CGPointMake(0, 0);
  // I verified that the content size is always greater than
  // CGSizeZero (both width and height)
  CGFloat contentWidth = [self calculateNewContentWidth];
  self.contentSize = CGSizeMake(contentWidth, self.frame.size.height);
  // Here is the problem: Sometimes this triggers layoutSubviews,
  // sometimes it does not.
  [self setNeedsLayout];
  // Adding the following line for debugging purposes does not
  // help, making it clear that setNeedsLayout has no effect.
  //   [self layoutIfNeeded];
}

, : , :

  • layoutSubviews , . layoutSubviews - not, , . , UIScrollView, .. layoutSubviews UIScrollView, . , , "" - layoutSubviews , , .
  • layoutSubviews , UILabel subviews
  • layoutSubviews , , Auto Layout subviews

, Auto Layout - , , setNeedsLayout.

+4

Source: https://habr.com/ru/post/1542511/


All Articles