MASViewConstraint.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // MASViewConstraint.m
  3. // Masonry
  4. //
  5. // Created by Jonas Budelmann on 20/07/13.
  6. // Copyright (c) 2013 cloudling. All rights reserved.
  7. //
  8. #import "MASViewConstraint.h"
  9. #import "MASConstraint+Private.h"
  10. #import "MASCompositeConstraint.h"
  11. #import "MASLayoutConstraint.h"
  12. #import "View+MASAdditions.h"
  13. #import <objc/runtime.h>
  14. @interface MAS_VIEW (MASConstraints)
  15. @property (nonatomic, readonly) NSMutableSet *mas_installedConstraints;
  16. @end
  17. @implementation MAS_VIEW (MASConstraints)
  18. static char kInstalledConstraintsKey;
  19. - (NSMutableSet *)mas_installedConstraints {
  20. NSMutableSet *constraints = objc_getAssociatedObject(self, &kInstalledConstraintsKey);
  21. if (!constraints) {
  22. constraints = [NSMutableSet set];
  23. objc_setAssociatedObject(self, &kInstalledConstraintsKey, constraints, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  24. }
  25. return constraints;
  26. }
  27. @end
  28. @interface MASViewConstraint ()
  29. @property (nonatomic, strong, readwrite) MASViewAttribute *secondViewAttribute;
  30. @property (nonatomic, weak) MAS_VIEW *installedView;
  31. @property (nonatomic, weak) MASLayoutConstraint *layoutConstraint;
  32. @property (nonatomic, assign) NSLayoutRelation layoutRelation;
  33. @property (nonatomic, assign) MASLayoutPriority layoutPriority;
  34. @property (nonatomic, assign) CGFloat layoutMultiplier;
  35. @property (nonatomic, assign) CGFloat layoutConstant;
  36. @property (nonatomic, assign) BOOL hasLayoutRelation;
  37. @property (nonatomic, strong) id mas_key;
  38. @property (nonatomic, assign) BOOL useAnimator;
  39. @end
  40. @implementation MASViewConstraint
  41. - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute {
  42. self = [super init];
  43. if (!self) return nil;
  44. _firstViewAttribute = firstViewAttribute;
  45. self.layoutPriority = MASLayoutPriorityRequired;
  46. self.layoutMultiplier = 1;
  47. return self;
  48. }
  49. #pragma mark - NSCoping
  50. - (id)copyWithZone:(NSZone __unused *)zone {
  51. MASViewConstraint *constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:self.firstViewAttribute];
  52. constraint.layoutConstant = self.layoutConstant;
  53. constraint.layoutRelation = self.layoutRelation;
  54. constraint.layoutPriority = self.layoutPriority;
  55. constraint.layoutMultiplier = self.layoutMultiplier;
  56. constraint.delegate = self.delegate;
  57. return constraint;
  58. }
  59. #pragma mark - Public
  60. + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view {
  61. return [view.mas_installedConstraints allObjects];
  62. }
  63. #pragma mark - Private
  64. - (void)setLayoutConstant:(CGFloat)layoutConstant {
  65. _layoutConstant = layoutConstant;
  66. #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
  67. if (self.useAnimator) {
  68. [self.layoutConstraint.animator setConstant:layoutConstant];
  69. } else {
  70. self.layoutConstraint.constant = layoutConstant;
  71. }
  72. #else
  73. self.layoutConstraint.constant = layoutConstant;
  74. #endif
  75. }
  76. - (void)setLayoutRelation:(NSLayoutRelation)layoutRelation {
  77. _layoutRelation = layoutRelation;
  78. self.hasLayoutRelation = YES;
  79. }
  80. - (BOOL)supportsActiveProperty {
  81. return [self.layoutConstraint respondsToSelector:@selector(isActive)];
  82. }
  83. - (BOOL)isActive {
  84. BOOL active = YES;
  85. if ([self supportsActiveProperty]) {
  86. active = [self.layoutConstraint isActive];
  87. }
  88. return active;
  89. }
  90. - (BOOL)hasBeenInstalled {
  91. return (self.layoutConstraint != nil) && [self isActive];
  92. }
  93. - (void)setSecondViewAttribute:(id)secondViewAttribute {
  94. if ([secondViewAttribute isKindOfClass:NSValue.class]) {
  95. [self setLayoutConstantWithValue:secondViewAttribute];
  96. } else if ([secondViewAttribute isKindOfClass:MAS_VIEW.class]) {
  97. _secondViewAttribute = [[MASViewAttribute alloc] initWithView:secondViewAttribute layoutAttribute:self.firstViewAttribute.layoutAttribute];
  98. } else if ([secondViewAttribute isKindOfClass:MASViewAttribute.class]) {
  99. MASViewAttribute *attr = secondViewAttribute;
  100. if (attr.layoutAttribute == NSLayoutAttributeNotAnAttribute) {
  101. _secondViewAttribute = [[MASViewAttribute alloc] initWithView:attr.view item:attr.item layoutAttribute:self.firstViewAttribute.layoutAttribute];;
  102. } else {
  103. _secondViewAttribute = secondViewAttribute;
  104. }
  105. } else {
  106. NSAssert(NO, @"attempting to add unsupported attribute: %@", secondViewAttribute);
  107. }
  108. }
  109. #pragma mark - NSLayoutConstraint multiplier proxies
  110. - (MASConstraint * (^)(CGFloat))multipliedBy {
  111. return [^id(CGFloat multiplier) {
  112. NSAssert(!self.hasBeenInstalled,
  113. @"Cannot modify constraint multiplier after it has been installed");
  114. self.layoutMultiplier = multiplier;
  115. return self;
  116. } copy];
  117. }
  118. - (MASConstraint * (^)(CGFloat))dividedBy {
  119. return [^id(CGFloat divider) {
  120. NSAssert(!self.hasBeenInstalled,
  121. @"Cannot modify constraint multiplier after it has been installed");
  122. self.layoutMultiplier = 1.0/divider;
  123. return self;
  124. } copy];
  125. }
  126. #pragma mark - MASLayoutPriority proxy
  127. - (MASConstraint * (^)(MASLayoutPriority))priority {
  128. return [^id(MASLayoutPriority priority) {
  129. NSAssert(!self.hasBeenInstalled,
  130. @"Cannot modify constraint priority after it has been installed");
  131. self.layoutPriority = priority;
  132. return self;
  133. } copy];
  134. }
  135. #pragma mark - NSLayoutRelation proxy
  136. - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation {
  137. return [^id(id attribute, NSLayoutRelation relation) {
  138. if ([attribute isKindOfClass:NSArray.class]) {
  139. NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation");
  140. NSMutableArray *children = NSMutableArray.new;
  141. for (id attr in attribute) {
  142. MASViewConstraint *viewConstraint = [self copy];
  143. viewConstraint.layoutRelation = relation;
  144. viewConstraint.secondViewAttribute = attr;
  145. [children addObject:viewConstraint];
  146. }
  147. MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
  148. compositeConstraint.delegate = self.delegate;
  149. [self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint];
  150. return compositeConstraint;
  151. } else {
  152. NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation");
  153. self.layoutRelation = relation;
  154. self.secondViewAttribute = attribute;
  155. return self;
  156. }
  157. } copy];
  158. }
  159. #pragma mark - Semantic properties
  160. - (MASConstraint *)with {
  161. return self;
  162. }
  163. - (MASConstraint *)and {
  164. return self;
  165. }
  166. #pragma mark - attribute chaining
  167. - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
  168. NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation");
  169. return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
  170. }
  171. #pragma mark - Animator proxy
  172. #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
  173. - (MASConstraint *)animator {
  174. self.useAnimator = YES;
  175. return self;
  176. }
  177. #endif
  178. #pragma mark - debug helpers
  179. - (MASConstraint * (^)(id))key {
  180. return [^id(id key) {
  181. self.mas_key = key;
  182. return self;
  183. } copy];
  184. }
  185. #pragma mark - NSLayoutConstraint constant setters
  186. - (void)setInsets:(MASEdgeInsets)insets {
  187. NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
  188. switch (layoutAttribute) {
  189. case NSLayoutAttributeLeft:
  190. case NSLayoutAttributeLeading:
  191. self.layoutConstant = insets.left;
  192. break;
  193. case NSLayoutAttributeTop:
  194. self.layoutConstant = insets.top;
  195. break;
  196. case NSLayoutAttributeBottom:
  197. self.layoutConstant = -insets.bottom;
  198. break;
  199. case NSLayoutAttributeRight:
  200. case NSLayoutAttributeTrailing:
  201. self.layoutConstant = -insets.right;
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. - (void)setInset:(CGFloat)inset {
  208. [self setInsets:(MASEdgeInsets){.top = inset, .left = inset, .bottom = inset, .right = inset}];
  209. }
  210. - (void)setOffset:(CGFloat)offset {
  211. self.layoutConstant = offset;
  212. }
  213. - (void)setSizeOffset:(CGSize)sizeOffset {
  214. NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
  215. switch (layoutAttribute) {
  216. case NSLayoutAttributeWidth:
  217. self.layoutConstant = sizeOffset.width;
  218. break;
  219. case NSLayoutAttributeHeight:
  220. self.layoutConstant = sizeOffset.height;
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. - (void)setCenterOffset:(CGPoint)centerOffset {
  227. NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
  228. switch (layoutAttribute) {
  229. case NSLayoutAttributeCenterX:
  230. self.layoutConstant = centerOffset.x;
  231. break;
  232. case NSLayoutAttributeCenterY:
  233. self.layoutConstant = centerOffset.y;
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. #pragma mark - MASConstraint
  240. - (void)activate {
  241. [self install];
  242. }
  243. - (void)deactivate {
  244. [self uninstall];
  245. }
  246. - (void)install {
  247. if (self.hasBeenInstalled) {
  248. return;
  249. }
  250. if ([self supportsActiveProperty] && self.layoutConstraint) {
  251. self.layoutConstraint.active = YES;
  252. [self.firstViewAttribute.view.mas_installedConstraints addObject:self];
  253. return;
  254. }
  255. MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item;
  256. NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute;
  257. MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item;
  258. NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute;
  259. // alignment attributes must have a secondViewAttribute
  260. // therefore we assume that is refering to superview
  261. // eg make.left.equalTo(@10)
  262. if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) {
  263. secondLayoutItem = self.firstViewAttribute.view.superview;
  264. secondLayoutAttribute = firstLayoutAttribute;
  265. }
  266. MASLayoutConstraint *layoutConstraint
  267. = [MASLayoutConstraint constraintWithItem:firstLayoutItem
  268. attribute:firstLayoutAttribute
  269. relatedBy:self.layoutRelation
  270. toItem:secondLayoutItem
  271. attribute:secondLayoutAttribute
  272. multiplier:self.layoutMultiplier
  273. constant:self.layoutConstant];
  274. layoutConstraint.priority = self.layoutPriority;
  275. layoutConstraint.mas_key = self.mas_key;
  276. if (self.secondViewAttribute.view) {
  277. MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view];
  278. NSAssert(closestCommonSuperview,
  279. @"couldn't find a common superview for %@ and %@",
  280. self.firstViewAttribute.view, self.secondViewAttribute.view);
  281. self.installedView = closestCommonSuperview;
  282. } else if (self.firstViewAttribute.isSizeAttribute) {
  283. self.installedView = self.firstViewAttribute.view;
  284. } else {
  285. self.installedView = self.firstViewAttribute.view.superview;
  286. }
  287. MASLayoutConstraint *existingConstraint = nil;
  288. if (self.updateExisting) {
  289. existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint];
  290. }
  291. if (existingConstraint) {
  292. // just update the constant
  293. existingConstraint.constant = layoutConstraint.constant;
  294. self.layoutConstraint = existingConstraint;
  295. } else {
  296. [self.installedView addConstraint:layoutConstraint];
  297. self.layoutConstraint = layoutConstraint;
  298. [firstLayoutItem.mas_installedConstraints addObject:self];
  299. }
  300. }
  301. - (MASLayoutConstraint *)layoutConstraintSimilarTo:(MASLayoutConstraint *)layoutConstraint {
  302. // check if any constraints are the same apart from the only mutable property constant
  303. // go through constraints in reverse as we do not want to match auto-resizing or interface builder constraints
  304. // and they are likely to be added first.
  305. for (NSLayoutConstraint *existingConstraint in self.installedView.constraints.reverseObjectEnumerator) {
  306. if (![existingConstraint isKindOfClass:MASLayoutConstraint.class]) continue;
  307. if (existingConstraint.firstItem != layoutConstraint.firstItem) continue;
  308. if (existingConstraint.secondItem != layoutConstraint.secondItem) continue;
  309. if (existingConstraint.firstAttribute != layoutConstraint.firstAttribute) continue;
  310. if (existingConstraint.secondAttribute != layoutConstraint.secondAttribute) continue;
  311. if (existingConstraint.relation != layoutConstraint.relation) continue;
  312. if (existingConstraint.multiplier != layoutConstraint.multiplier) continue;
  313. if (existingConstraint.priority != layoutConstraint.priority) continue;
  314. return (id)existingConstraint;
  315. }
  316. return nil;
  317. }
  318. - (void)uninstall {
  319. if ([self supportsActiveProperty]) {
  320. self.layoutConstraint.active = NO;
  321. [self.firstViewAttribute.view.mas_installedConstraints removeObject:self];
  322. return;
  323. }
  324. [self.installedView removeConstraint:self.layoutConstraint];
  325. self.layoutConstraint = nil;
  326. self.installedView = nil;
  327. [self.firstViewAttribute.view.mas_installedConstraints removeObject:self];
  328. }
  329. @end