Self sizing Cells,. Autolayout and hidden UIViews

本小妞迷上赌 提交于 2020-01-16 00:52:24

问题


i am using Autolayout to set automatically the height of a cell. But i want to use some hidden UIViews in my Cells. So in some rows they should be hidden. The problem here is, if the label is still hidden it gets recognized by autolayout and it uses the space.

So i guess i need to update the height of my label, or the height of my Constraints.

What is the correct way if i am using Autolayout to hide UIElements in a Cell?

How can i update an existing (from Storyboard) Constraint?

Edit: Found out that i am able to update Constraints with Storyboard, is that the correct way to handle that? - but which is the correct one to setup the height? (This is just a Demo Project)


回答1:


This is standard task with autolayout. Currently, there are two ways of solving this task:

  1. Removing hidden views
  2. Changing constraints priority

Removing of hidden views

Looks pretty easy and you won't have any problems if you don't use UITableView (you have to put it back in prepareForReuse), UICollectionView or your data can appear while you're on screen, where you've just removed views.

Removing of views is "heavy" operation, so you need to think twice, if you pick this way.

In CustomCell.m:

- (void) prepareForReuse {
 [super prepareForReuse];
 // add removed views again
 // establish constraints
}

- (void) configureCell {
 // check if you need to hide a view
 [view removeFromSuperview];
}

Changing constraints priority

Assume, you have following cell:


View A

View B


If B sometimes can be hidden, then specify for View A following constraints: Space to B with 750 Bottom Space to superview with 500

If you set B as hidden, change space to B = 500, bottom space to superview = 750.

Undo that change in prepareForReuse



来源:https://stackoverflow.com/questions/26178454/self-sizing-cells-autolayout-and-hidden-uiviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!