Skip to content

Instantly share code, notes, and snippets.

@iwill
Created November 27, 2017 05:04
Show Gist options
  • Save iwill/a479deea1454b4415f3c82b4c1ef9e5c to your computer and use it in GitHub Desktop.
Save iwill/a479deea1454b4415f3c82b4c1ef9e5c to your computer and use it in GitHub Desktop.
A Masonry Extension supports iOS 11 safeAreaLayoutGuide, @see https://github.com/SnapKit/Masonry/pull/473
//
// Masonry+M9Ext.h
// M9Dev
//
// Created by MingLQ on 2017-04-05.
// Copyright © 2017 MingLQ <[email protected]>. Released under the MIT license.
//
#import <Masonry/Masonry.h>
NS_ASSUME_NONNULL_BEGIN
/**
@see https://github.com/SnapKit/Masonry/pull/473
*/
@interface MAS_VIEW (M9Ext)
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuide;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideLeading;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideTrailing;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideLeft;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideRight;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideTop;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideBottom;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideWidth;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideHeight;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideCenterX;
@property (nonatomic, readonly, nullable) MASViewAttribute *m9_safeAreaLayoutGuideCenterY;
@end
NS_ASSUME_NONNULL_END
//
// Masonry+M9Ext.m
// M9Dev
//
// Created by MingLQ on 2017-04-05.
// Copyright © 2017 MingLQ <[email protected]>. Released under the MIT license.
//
#import <objc/runtime.h>
#import "Masonry+M9Ext.h"
NS_ASSUME_NONNULL_BEGIN
static inline void m9_swizzleSelector(Class theClass, SEL originalSelector, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(theClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
}
#pragma mark -
@interface MASViewConstraint (M9Ext)
@property (nonatomic, strong, readwrite) MASViewAttribute *secondViewAttribute;
@end
@implementation MASViewConstraint (M9Ext)
+ (void)load {
m9_swizzleSelector([self class],
@selector(setSecondViewAttribute:),
@selector(m9_setSecondViewAttribute:));
}
@dynamic secondViewAttribute;
- (void)m9_setSecondViewAttribute:(id)secondViewAttribute {
if ([secondViewAttribute isKindOfClass:MASViewAttribute.class]) {
MASViewAttribute *attr = secondViewAttribute;
if (attr.layoutAttribute == NSLayoutAttributeNotAnAttribute) {
secondViewAttribute = [[MASViewAttribute alloc] initWithView:attr.view item:attr.item layoutAttribute:self.firstViewAttribute.layoutAttribute];;
}
}
[self m9_setSecondViewAttribute:secondViewAttribute];
}
@end
#pragma mark -
@implementation MAS_VIEW (M9Ext)
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuide {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeNotAnAttribute];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideLeading {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeLeading];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideTrailing {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeTrailing];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideLeft {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeLeft];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideRight {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeRight];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideTop {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeTop];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideBottom {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeBottom];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideWidth {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeWidth];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideHeight {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeHeight];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideCenterX {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeCenterX];
}
#endif
return nil;
}
- (nullable MASViewAttribute *)m9_safeAreaLayoutGuideCenterY {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000)
if (@available(iOS 11.0, *)) {
return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeCenterY];
}
#endif
return nil;
}
@end
NS_ASSUME_NONNULL_END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment