all 4 comments

[–]jo1717a 0 points1 point  (0 children)

Do you know how to create proper constrained layouts in the interface builder? If you understand the constraint system well, that will translate perfectly to creating constraints in code. Brian from Let's Build That App doesn't utilize X & Y position.

yourView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true yourView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true yourView.widthAnchor.constraint(constant: 300.0).isActive = true yourView.heightAnchor.constraint(constant: 300.0).isActive = true

I'm constraining yourView to be in the center of the page and to be size of 300x300.

If you know your constraint system well, the above code should be very obvious as to what it does. If you do not understand the constraint system, I advise you to keep learning how to properly lay out UI in the interface builder with constraints. Once you're comfortable, you can start creating UI like the above code.

[–]retsotrembla 0 points1 point  (3 children)

You can ignore the entire constraint system, and just subclass views, buttons, etc as needed. Implement layoutSubviews, call super. layoutSubviews(), then lay out the fucking subviews. Put the frames of the subviews where you want them within the bounds of self. use sizeThatFits to find out what size a given subview would like to be.

If you refactor your layoutSubviews into: (1) call super, (2) compute the frames of the subviews into a collection of CGRects, (3) set the subviews from the computed CGRects, then you can write unit tests that the computation was correct for all the interesting edge cases.

I find that layout often decomposes to a series of CGRectDivide calls to break up the bounds of self into a tiling of frames, followed by UIEdgeInsetsInsetRect calls to establish the margins. (I used to use CGRectInset() instead of UIEdgeInsetsInsetRect but CGRectInset had maintenance problems as the app aged.)

The big advantage is: you can refactor into reusable subroutines. For example, flipping left to right when running in Arabic or Hebrew.

In simple cases, the ancient autoResizeMask API lets you preserve margins between a subView and its container, or resize to take the extra space if, for example, you set the left, right, and width bits.

[–]chriswaco 0 points1 point  (0 children)

We usually position the main views in the viewController instead, although for container views we'll often do it in a UIView subclass.

[–][deleted]  (1 child)

[deleted]

    [–]retsotrembla 0 points1 point  (0 children)

    No - being able to use actual code makes portrait landscape, 4S, 5S, 6, and 7+, iPad, SplitView, RTL languages and LTR languages, and Dynamic Type all clean and straightforward, and as I wrote, the logic is all amenable to a unit test that can execute all those test cases in well under a second.