2

What is the best way to remove auto layout constraints for a Cell when a view should not be shown?

We have a cell which has a layout with around 6-7 views. One of those views is for a star rating. When the star rating is not available we do not want to show the view. At present we hide the view but this leaves the auto layout constraints in place.

Similar question - How to use auto-layout to move other views when a view is hidden?

enter image description here

This is the view in question hilighted above. We would ideally like to remove this view from its superview when there is no available star rating. The issue we have is that if we remove the view from superview removeFromSuperview in cellForRow... then the next cell would be affected, because the view is not added again.

Community
  • 1
  • 1
StuartM
  • 6,743
  • 18
  • 84
  • 160
  • OR would the best option be to just create another cell without the subview and use the different cells depending if the value exists? – StuartM Jun 09 '14 at 13:56
  • How about subview.alpha = 0.0 or subview.hidden = YES ? – danh Jun 09 '14 at 15:53
  • As I said setting the view to hidden will not do anything the same applies to alpha you are just hiding the view not removing it therefore the frame and constraints will remain. – StuartM Jun 09 '14 at 16:54

3 Answers3

0

(I would comment to request clarification, but do not yet have the reputation.)

Is there a chance you could just hide the view for the cell in question?

If I understand what you are saying, you are suggesting that removing the view in question from it's superview creates an issue when you are creating a new cell. So, when you dequeue a new cell, just check your star-count property, and if it is >0 for this next cell, then show the view for that cell.

Another option is to pin the surrounding views to their parent view rather than pinning them to this view that you want to remove. This way, when you remove the star-rating view, the layout constraints for the surrounding views remain unchanged.

joelg
  • 1,094
  • 9
  • 19
  • Hiding and showing the view does not resolve the issue. As the frame for the view remains the same and the relevant constraints still exist for the hidden view, that is why you have to removeFromSuperview instead. – StuartM Jun 09 '14 at 13:55
  • So you are wanting to remove the view, but you don't want it to affect the surrounding views? – joelg Jun 09 '14 at 14:30
  • The issue/question is the best way to handle auto layout. If there is no rating I do not want this view to be shown. I can set it to hidden but the label next to it would not pin to the left edge of the cell this is because the frame/constraints for the hidden view still exist. I cannot removeFromSuperview in case there is another cell after that requires the view as it is then removed from the dequeued variant of the cell forever. I think the best way might be to create two seperate cell layouts in IB then use whichever based on the rating having a value or not. – StuartM Jun 09 '14 at 15:07
  • I think there is an easier way. I would just pin the surrounding views to the parent view as opposed to the view that you want to remove. I updated my answer to include this option. – joelg Jun 09 '14 at 15:49
  • When you create constraints in IB you pin a view to another. I.e. how would you pin the second label to the left side of the content view? Further to this, if the rating view exists the constraint constant would be one value, then if it doesn't it would be another. So you'd have to find then update that constraints which is even more work. I think the best way is to create separate cells but Ill leave the question open unless someone else knows of other ways – StuartM Jun 09 '14 at 16:56
0

remember the constraint (for show/hide a view) in to variable and remove it constrains. and delete this constrains and replace with new constrains if you need change view.

For example:

view has width and height for show and replace constraint where width and height will be zero for hide view.

Bimawa
  • 3,535
  • 2
  • 25
  • 45
0

The best route I found was to create separate cell layouts and decide which layout to use based on if the information was available. This meant creating a second prototype cell in IB without the view in question and different constraints but works as expected. Open to other suggestions on this one.

StuartM
  • 6,743
  • 18
  • 84
  • 160