2

So I have a solid understanding of the differences between the frame and alignment rect on UIView. However, in practice the concept doesn't seem to work.

I override alignmentRectInsets which will inset the frame according to the content that I want the constraints to apply too. I see that the frame is adjusted accordingly; however, the constraints ignore the alignment rect and still use the original frame values. What am I missing? I thought the point of the alignment rect was to be able to adjust how the constraints will align to specific content. In this instance, I want it to ignore a "badge" that I have in the upper-right corner. Adjusting the insets just doesn't work the way I interpreted it...it seems like the alignmentRectInsets property will simply change the frame, but not impact the constraints.

In addition, I tried to override the methods "alignmentRectForFrame" and "frameForAlignmentRect". Unfortunately, these methods aren't called when the view is setup so I can't adjust the alignment rect that way.

I do have "translatesAutoResizingMask" set to false. The views and constraints are setup in Interface Builder. I'm using Xcode 7.1 and running iOS 9.1. However, I saw the same issues with Xcode 7 and iOS 9 before upgrading yesterday.

Any help or clarification on alignment rect would be great. Thanks!

JamWils
  • 785
  • 7
  • 17

1 Answers1

0

One thing that was confusing to me was that when adding constraints to your subviews, "superview" really represents the alignment rect and not your view's frame. So when you're constraining your badge you want it to be up and to the right from your superview, not constrained to its top-right corner. If that isn't clear you might want to check out this blog post.

jefflovejapan
  • 2,047
  • 3
  • 20
  • 34