P. 57: Alignment rectangle


#1

I thought this section was super confusing. And after this section, the text reverted to using the term frame, when I thought the correct term should have been alignment rectangle.

I would humbly suggest to the authors that we beginners do not need to know that a frame and an alignment rectangle are different at this stage.


#2

Thanks for the suggestion! I understand how it could be confusing.

— The frame is the actual position and size for a view. “Where is this view located?”
— The alignment rectangle is used by Auto Layout to determine what the frame should be. So when centering the checkbox in Figure 3.17, you’d want to center it based on the “circle,” not on the entire view. The alignment rectangle allows this to happen.

Those are very basic descriptions. Let me know if you need more clarification!


#3

Hi,

Thanks for the response. I thought the diagrams explained things perfectly, however after the diagrams the text says:

That left me confused as to how you would ‘create’ the alignment triangle on the right in the diagram v. the one on the left.


#4

Good question. There are two things to say on this. First, a clarification, and then second, expanding on the alignment rect.

To clarify what the text is trying to say: with the frame, you can set it directly:

But you can’t do that for the alignment rectangle. The alignment rectangle is not set directly; instead, it’s used, along with auto layout constraints, in order to derive the frame.

Now to expand on the alignment rect: Customizing the alignment rect almost exclusively happens when you create a new UIView subclass – some view that Apple did not create for you. The checkbox is a pretty good example. You’d create a subclass, class Checkbox: UIView { … }, and then in its implementation, you would implement the UIView method alignmentRectInsets():

override func alignmentRectInsets() -> UIEdgeInsets { return UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 20) }

Be default, that methods returns a UIEdgeInset with all 0s. In other words, the default is no insets which would make the alignment rectangle the same as the frame.


#5

Hi - I also have a question on P57. It says to Select the background view in order to change the background color. In Xcode version 9.3, I don’t see where I can select the background view - kinda stuck at this point.
Any help would be appreciated!

Thanks…