我们在这里稍微扩展了视觉格式化语言,所以现在您可以锁定“<|” 当您的意思是安全区域布局指南时。我希望苹果能做这样的事情。
例如,如果您有以下 iOS 11 之前的代码:
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[_button]-(normalPadding)-|"
options:0 metrics:metrics views:views
]];
现在您要确保按钮位于 iPhone X 上的安全底部边缘上方,然后执行以下操作:
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint
mmm_constraintsWithVisualFormat:@"V:[_button]-(normalPadding)-<|"
options:0 metrics:metrics views:views
]];
就是这样。它会在 iOS 9 和 10 上将按钮锚定到其 superview 的底部,但在 iOS 11 上将其锚定到 safeAreaLayoutGuide 的底部。
请注意,使用“|>”固定到顶部不会排除 iOS 9 和 10 上的状态栏。
// In @interface/@implementation NSLayoutConstraint (MMMUtil)
// ...
+(NSArray<NSLayoutConstraint *> *)mmm_constraintsWithVisualFormat:(NSString *)format
options:(NSLayoutFormatOptions)opts
metrics:(NSDictionary<NSString *,id> *)metrics
views:(NSDictionary<NSString *,id> *)views
{
if ([format rangeOfString:@"<|"].location == NSNotFound && [format rangeOfString:@"|>"].location == NSNotFound ) {
// No traces of our special symbol, so do nothing special.
return [self constraintsWithVisualFormat:format options:opts metrics:metrics views:views];
}
if (![UIView instancesRespondToSelector:@selector(safeAreaLayoutGuide)]) {
// Before iOS 11 simply use the edges of the corresponding superview.
NSString *actualFormat = [format stringByReplacingOccurrencesOfString:@"<|" withString:@"|"];
actualFormat = [actualFormat stringByReplacingOccurrencesOfString:@"|>" withString:@"|"];
return [NSLayoutConstraint constraintsWithVisualFormat:actualFormat options:opts metrics:metrics views:views];
}
//
// OK, iOS 11+ time.
// For simplicity we replace our special symbols with a reference to a stub view, feed the updated format string
// to the system, and then replace every reference to our stub view with a corresponding reference to safeAreaLayoutGuide.
//
UIView *stub = [[UIView alloc] init];
static NSString * const stubKey = @"__MMMLayoutStub";
NSString *stubKeyRef = [NSString stringWithFormat:@"[%@]", stubKey];
NSDictionary *extendedViews = [@{ stubKey : stub } mmm_extendedWithDictionary:views];
NSString *actualFormat = [format stringByReplacingOccurrencesOfString:@"<|" withString:stubKeyRef];
actualFormat = [actualFormat stringByReplacingOccurrencesOfString:@"|>" withString:stubKeyRef];
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:actualFormat options:opts metrics:metrics views:extendedViews];
NSMutableArray *processedConstraints = [[NSMutableArray alloc] init];
for (NSLayoutConstraint *c in constraints) {
UIView *firstView = c.firstItem;
UIView *secondView = c.secondItem;
NSLayoutConstraint *processed;
if (firstView == stub) {
if (![secondView isKindOfClass:[UIView class]]) {
NSAssert(NO, @"We only support UIView with <| and |> anchors, got %@", secondView.class);
continue;
}
processed = [self
constraintWithItem:secondView.superview.safeAreaLayoutGuide attribute:_MMMOppositeAttribute(c.firstAttribute)
relatedBy:c.relation
toItem:secondView attribute:c.secondAttribute
multiplier:c.multiplier constant:c.constant
priority:c.priority
identifier:@"MMMSafeAreaFirstItemConstraint"
];
} else if (secondView == stub && [firstView isKindOfClass:[UIView class]]) {
if (![firstView isKindOfClass:[UIView class]]) {
NSAssert(NO, @"We only support UIView with <| and |> anchors, got %@", secondView.class);
continue;
}
processed = [self
constraintWithItem:firstView attribute:c.firstAttribute
relatedBy:c.relation
toItem:firstView.superview.safeAreaLayoutGuide attribute:_MMMOppositeAttribute(c.secondAttribute)
multiplier:c.multiplier constant:c.constant
priority:c.priority
identifier:@"MMMSafeAreaSecondItemConstraint"
];
} else {
processed = c;
}
[processedConstraints addObject:processed];
}
return processedConstraints;
}
+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation
toItem:(id)view2 attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier constant:(CGFloat)c
priority:(UILayoutPriority)priority
identifier:(NSString *)identifier
{
NSLayoutConstraint *result = [NSLayoutConstraint constraintWithItem:view1 attribute:attr1 relatedBy:relation toItem:view2 attribute:attr2 multiplier:multiplier constant:c];
result.priority = priority;
result.identifier = identifier;
return result;
}
// @end
static inline NSLayoutAttribute _MMMOppositeAttribute(NSLayoutAttribute a) {
switch (a) {
// TODO: support trailing/leading in the same way
case NSLayoutAttributeLeft:
return NSLayoutAttributeRight;
case NSLayoutAttributeRight:
return NSLayoutAttributeLeft;
case NSLayoutAttributeTop:
return NSLayoutAttributeBottom;
case NSLayoutAttributeBottom:
return NSLayoutAttributeTop;
// These two are special cases, we see them when align all X or Y flags are used.
case NSLayoutAttributeCenterY:
return NSLayoutAttributeCenterY;
case NSLayoutAttributeCenterX:
return NSLayoutAttributeCenterX;
// Nothing more.
default:
NSCAssert(NO, @"We don't expect other attributes here");
return a;
}
}
@interface NSDictionary (MMMUtil)
- (NSDictionary *)mmm_extendedWithDictionary:(NSDictionary *)d;
@end
@implementation NSDictionary (MMMUtil)
- (NSDictionary *)mmm_extendedWithDictionary:(NSDictionary *)d {
if (!d || [d count] == 0)
return self;
NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithDictionary:self];
[result addEntriesFromDictionary:d];
return result;
}
@end