Set UITableView’s height to the height of its content with Auto Layout

You have to override updateViewConstraints() in your UIViewController and set the height constraint’s constant to tableView.contentSize.height: override func updateViewConstraints() { tableHeightConstraint.constant = tableView.contentSize.height super.updateViewConstraints() } Then you have to make sure that Label2 has a top constraint that is greaterThanOrEqual to the table view’s bottom. And you also have to change the table view’s height … Read more

iOS change auto layout constraints when device rotates

In willRotateToInterfaceOrientation:duration:, send setNeedsUpdateConstraints to any view that needs its constraints modified. Alternatively, make a UIView subclass. In your subclass, register to receive UIApplicationWillChangeStatusBarOrientationNotification. When you receive the notification, send yourself setNeedsUpdateConstraints. This sets the needsUpdateConstraints flag on the view. Before the system performs layout (by sending layoutSubviews messages), it sends an updateConstraints message to … Read more

UICollectionView autosize height

I solved this eventually by fixing all Auto Layout issues, fixing the height of the collection view using a constraint. Then, whenever I know the content has changed I update the value of the constraint using the value collectionView.contentSize.height: self.verticalLayoutConstraint.constant = self.collectionView.collectionViewLayout.collectionViewContentSize.height; Then the collection view is resized properly and it behaves nicely within the … Read more

Animate intrinsicContentSize changes

invalidateIntrinsicContentSize works well with animations and layoutIfNeeded. The only thing you need to consider is, that changing the intrinsic content size invalidates the layout of the superview. So this should work: [UIView animateWithDuration:0.2 animations:^{ [self invalidateIntrinsicContentSize]; [self.superview setNeedsLayout]; [self.superview layoutIfNeeded]; }];

What is _UITemporaryLayoutWidth and why does it break my constraints?

So, I’m not sure if this helps you with your problem since it sounds like you’re building your layout in IB, but here’s an issue that I just ran into that may help others that google search for “_UITemporaryLayoutWidth”. My scenario was that I was adding auto layout constraints during init and was accidentally triggering … Read more

UITableView within UIScrollView using autolayout

First of all, are those other views (siblings of the table view) strictly above and below the table view? If so, have you considered letting the table view scroll normally, and putting those outside views in the table view’s header and footer views? Then you don’t need the scroll view. Second, you may want to … Read more

Update height constraint programmatically

Instead of adding a new constraint, you need to modify the constant on your existing constraint. Use an IBOutlet to connect to your constraint in Interface Builder: @property (nonatomic, weak) NSLayoutConstraint *heightConstraint; Then, when you need to set it programmatically, simply set the constant property on the constraint: heightConstraint.constant = 100; OR If you can’t … Read more

tech