In interface builder I can get a "Vertical Space" that matches the "standard" spacing by 'pinning':
In the visual constraint language I can accomplish the same thing with:
V:[top]-[bottom]
How do I add standard spacing between two views in code?
I'm looking for something like this that might appear in a UIViewController:
NSLayoutConstraint *spacing = ???
[self.view addConstraint spacing];
Auto Layout defines your user interface using a series of constraints. Constraints typically represent a relationship between two views. Auto Layout then calculates the size and location of each view based on these constraints.
Different languages use different layout directions. English, for example, uses a left-to-right layout direction, and Arabic and Hebrew use a right-to-left layout direction. In general, the order of the user interface elements should match the layout direction.
Auto Layout is no exception. The rest of this guide is designed to help ease your transition to Auto Layout. The Auto Layout Without Constraints chapter describes a high-level abstraction that simplifies the creation of Auto Layout backed user interfaces.
Fortunately, mastering Auto Layout is no different from mastering any other programming task. There are two basic steps: First you need to understand the logic behind constraint-based layouts, and then you need to learn the API. You’ve successfully performed these steps when learning other programming tasks. Auto Layout is no exception.
As of iOS 11.0 (released in 2017), there are methods that use the system spacing:
@interface NSLayoutXAxisAnchor (UIViewDynamicSystemSpacingSupport)
/* Constraints of the form,
receiver [= | ≥ | ≤] 'anchor' + 'multiplier' * system space,
where the value of the system space is determined from information available from the anchors.
The constraint affects how far the receiver will be positioned trailing 'anchor', per the effective user interface layout direction.
*/
- (NSLayoutConstraint *)constraintEqualToSystemSpacingAfterAnchor:(NSLayoutXAxisAnchor *)anchor multiplier:(CGFloat)multiplier __attribute__((warn_unused_result)) API_AVAILABLE(macos(11.0),ios(11.0),tvos(11.0));
- (NSLayoutConstraint *)constraintGreaterThanOrEqualToSystemSpacingAfterAnchor:(NSLayoutXAxisAnchor *)anchor multiplier:(CGFloat)multiplier __attribute__((warn_unused_result)) API_AVAILABLE(macos(11.0),ios(11.0),tvos(11.0));
- (NSLayoutConstraint *)constraintLessThanOrEqualToSystemSpacingAfterAnchor:(NSLayoutXAxisAnchor *)anchor multiplier:(CGFloat)multiplier __attribute__((warn_unused_result)) API_AVAILABLE(macos(11.0),ios(11.0),tvos(11.0));
@end
@interface NSLayoutYAxisAnchor (UIViewDynamicSystemSpacingSupport)
/* Constraints of the form,
receiver [= | ≥ | ≤] 'anchor' + 'multiplier' * system space,
where the value of the system space is determined from information available from the anchors.
The constraint affects how far the receiver will be positioned below 'anchor'.
If either the receiver or 'anchor' is the firstBaselineAnchor or lastBaselineAnchor of a view with text content
then the spacing will depend on the fonts involved and will change when those do.
*/
- (NSLayoutConstraint *)constraintEqualToSystemSpacingBelowAnchor:(NSLayoutYAxisAnchor *)anchor multiplier:(CGFloat)multiplier __attribute__((warn_unused_result)) API_AVAILABLE(macos(11.0),ios(11.0),tvos(11.0));
- (NSLayoutConstraint *)constraintGreaterThanOrEqualToSystemSpacingBelowAnchor:(NSLayoutYAxisAnchor *)anchor multiplier:(CGFloat)multiplier __attribute__((warn_unused_result)) API_AVAILABLE(macos(11.0),ios(11.0),tvos(11.0));
- (NSLayoutConstraint *)constraintLessThanOrEqualToSystemSpacingBelowAnchor:(NSLayoutYAxisAnchor *)anchor multiplier:(CGFloat)multiplier __attribute__((warn_unused_result)) API_AVAILABLE(macos(11.0),ios(11.0),tvos(11.0));
@end
In Swift, you could use them like this:
let topView: UIView = ...
let bottomView: UIView = ...
bottomView.topAnchor
.constraint(equalToSystemSpacingBelow: topView.bottomAnchor, multiplier: 1)
.isActive = true
let leadingView: UIView = ...
let trailingView: UIView = ...
trailingView.leadingAnchor
.constraint(equalToSystemSpacingAfter: leadingView.trailingAnchor, multiplier: 1)
.isActive = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With