Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//
// NSLayoutConstraint+Dynamic.h
//
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface NSLayoutConstraint (Dynamic)
@property (nonatomic, assign) IBInspectable CGFloat genericConstant;
@property (nonatomic, assign) IBInspectable CGFloat iPhoneGenericConstant;
@property (nonatomic, assign) IBInspectable CGFloat iPhonePortraitConstant;
@property (nonatomic, assign) IBInspectable CGFloat iPhoneLandscapeConstant;
@property (nonatomic, assign) IBInspectable CGFloat iPadGenericConstant;
@property (nonatomic, assign) IBInspectable CGFloat iPadPortraitConstant;
@property (nonatomic, assign) IBInspectable CGFloat iPadLandscapeConstant;
- (void)adjustConstraintConstants;
@end
@interface UIView (NSLayoutConstraint_Dynamic)
- (void)adjustConstraintConstants;
@end
//
// NSLayoutConstraint+Dynamic.m
//
#import "NSLayoutConstraint+Dynamic.h"
#import <objc/runtime.h>
static const void * const k_genericConstant = &k_genericConstant;
static const void * const k_iPhoneGenericConstant = &k_iPhoneGenericConstant;
static const void * const k_iPhonePortraitConstant = &k_iPhonePortraitConstant;
static const void * const k_iPhoneLandscapeConstant = &k_iPhoneLandscapeConstant;
static const void * const k_iPadGenericConstant = &k_iPadGenericConstant;
static const void * const k_iPadPortraitConstant = &k_iPadPortraitConstant;
static const void * const k_iPadLandscapeConstant = &k_iPadLandscapeConstant;
#define DEFINE_PROPERTY(getter__, setter__, key__) \
- (CGFloat)getter__ \
{ \
return (CGFloat)[objc_getAssociatedObject(self, key__) doubleValue]; \
} \
- (void)setter__:(CGFloat)value \
{ \
objc_setAssociatedObject(self, key__, [NSNumber numberWithDouble:(double)value], OBJC_ASSOCIATION_COPY_NONATOMIC); \
}
@implementation NSLayoutConstraint (Dynamic)
DEFINE_PROPERTY(genericConstant, setGenericConstant, k_genericConstant)
DEFINE_PROPERTY(iPhoneGenericConstant, setIPhoneGenericConstant, k_iPhoneGenericConstant)
DEFINE_PROPERTY(iPhonePortraitConstant, setIPhonePortraitConstant, k_iPhonePortraitConstant)
DEFINE_PROPERTY(iPhoneLandscapeConstant, setIPhoneLandscapeConstant, k_iPhoneLandscapeConstant)
DEFINE_PROPERTY(iPadGenericConstant, setIPadGenericConstant, k_iPadGenericConstant)
DEFINE_PROPERTY(iPadPortraitConstant, setIPadPortraitConstant, k_iPadPortraitConstant)
DEFINE_PROPERTY(iPadLandscapeConstant, setIPadLandscapeConstant, k_iPadLandscapeConstant)
- (void)adjustConstraintConstants
{
NSNumber *value = nil;
UIUserInterfaceIdiom idiom = UI_USER_INTERFACE_IDIOM();
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (idiom == UIUserInterfaceIdiomPhone)
{
if (UIInterfaceOrientationIsPortrait(orientation))
{
value = objc_getAssociatedObject(self, k_iPhonePortraitConstant);
}
else
{
value = objc_getAssociatedObject(self, k_iPhoneLandscapeConstant);
}
value = value ?: objc_getAssociatedObject(self, k_iPhoneGenericConstant);
}
else
{
if (UIInterfaceOrientationIsPortrait(orientation))
{
value = objc_getAssociatedObject(self, k_iPadPortraitConstant);
}
else
{
value = objc_getAssociatedObject(self, k_iPadLandscapeConstant);
}
value = value ?: objc_getAssociatedObject(self, k_iPadGenericConstant);
}
value = value ?: objc_getAssociatedObject(self, k_genericConstant);
if (value)
{
self.constant = value.doubleValue;
}
}
@end
@implementation UIView (NSLayoutConstraint_Dynamic)
- (void)adjustConstraintConstants
{
[self.constraints makeObjectsPerformSelector:@selector(adjustConstraintConstants)];
[self.subviews makeObjectsPerformSelector:@selector(adjustConstraintConstants)];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment