I'm using a header (supplementary view) in my UICollectionView
. On the header I have label which has a certain distance from the left side. I'm doing my calculation and setting the constant of a constraint. Everything does work as expected.
If the orientation is now changed, the labels have the old position. Now I need to update the constraints for all my headers. Calling invalidateLayout
doesn't update the constraints. How can I manually trigger the recalculation?
Edit:
This is how my layoutSubviews
does look like, where the recalculation takes place:
public override void LayoutSubviews ()
{
this.width = this.collectionViewSize.Width;
this.itemWidth = (nfloat)Math.Round(this.width / numberOfItemsInRow);
leftSpacing.Constant = this.itemWidth * this.referencePoint;
if (leftSpacing.Constant == 0)
leftSpacing.Constant = SectionHeader.MIN_SPACING;
base.LayoutSubviews ();
}
As you can see this is not Objective-C but you should be able to see what I'm doing. I take the width of the collection view (set on instantiation of the supplementary view) and then I calculate the width of cell which corresponds nearly to the real size of the cells. With the referencePoint I determine the position. Here you can see the setup of my constraints:
public SectionHeader (CGRect frame) : base (frame)
{
this.width = frame.Size.Width;
this.itemWidth = (nfloat)Math.Round(width / numberOfItemsInRow);
label = new UILabel (){
BackgroundColor = UIColor.White,
TextAlignment = UITextAlignment.Left
};
label.TranslatesAutoresizingMaskIntoConstraints = false;
AddSubview (label);
NSMutableDictionary viewsDictionary = new NSMutableDictionary();
viewsDictionary["label"] = label;
this.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[label]|",(NSLayoutFormatOptions)0,null,viewsDictionary));
leftSpacing = NSLayoutConstraint.Create(label, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, SectionHeader.MIN_SPACING);
leftSpacing.Priority = 250;
this.AddConstraint(leftSpacing);
this.AddConstraint(NSLayoutConstraint.Create(label, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, 0));
}
In this code the calculation for the distance from the left is nearly the same, except that frame has the height of the set headerReferenceSize
. The width should be OK for the initialisation. I'm using the full height for the label, I set up the leading space and I pin it to the right.