iOS 11.0(2017年にリリース)の時点で、システム間隔を使用する方法があります。
@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
Swiftでは、次のように使用できます。
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