Search code examples
iosuiviewautolayoutsetneedsdisplay

Reload UIView and UILabel Frame


I have two UILabels and a UIView in a nib file. This nib is the header to my collectionView. In my viewForSupplementaryElementOfKind: method, I need to change the frames of these three objects. They are being changed according to the size of header.personCollege label.

[header.personCollege sizeToFit];
CGRect boxFrame = header.boxView.frame;
boxFrame.origin.x = header.personCollege.frame.origin.x + header.personCollege.frame.size.width + 11;
header.boxView.frame = boxFrame;

CGRect yearFrame = header.personYear.frame;
yearFrame.origin.x = boxFrame.origin.x + boxFrame.size.width + 11;
header.personYear.frame = yearFrame;

[header.personYear setNeedsDisplay];
[header.personCollege setNeedsDisplay];
[header.boxView setNeedsDisplay];

The problem is, setNeedsDisplay does not change the frames. The frames are only changed after a call to reloadData. I need the frames changed instantly. I am also using autoLayout.


Solution

  • Since you are using AutoLayout, you should change 'constant' value of the constraints associated with the views, rather than directly changing their frames.

    You can start by creating an IBOutlets/IBOutletCollection to the constraints you want to change and then using those IBoutlets you can change their 'constant' value.