Overriding iOS Storyboard Limitations

How can you change the limits set in the storyboard?

I am trying to simulate a Twitter iOS profile page where the title is compressed when scrolling down.

I have a UIScrollView that takes up the whole view. Then I have a UITableView in a UIScrollView. In the storyboard, I have to set a fixed height for a UITableView. Thus, it has the restriction Equals Height = 350.

Then I try to resize it programmatically:

 tableView.frame = CGRectMake(0, 0, scrollView.frame.width, scrollView.frame.height - 30)

When I do this, it does not affect the size of the UITableView at all. I assume this is due to the limitations of the storyboard.

Am I missing something or do I need to do this programmatically to get started?

+4
source share
1 answer

Do not fight the restrictions set in the storyboard. Instead, you can create a constraint @IBOutletto a height constraint by finding it in the "Document Structure" controlview and separating it from your constraint to your code. Give him a name like tableHeightContraint.

@IBOutlet weak var tableHeightConstraint: NSLayoutConstraint!

Then, when you want to change the height UITableView, change the constantrestriction property :

tableHeightConstraint.constant = scrollView.frame.height - 30

As @BlackRider noted in the comments, sometimes you need to call the layout after the restrictions have been changed.

Vocation:

view.layoutIfNeeded()

will tell Auto Layout to apply the constraint if necessary. This is especially true if you are reviving change. In this case, the call layoutIfNeeded()is made inside the animation block.

UIView.animateWithDuration(2) {
    self.view.layoutIfNeeded()
}
+9
source

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


All Articles