MASViewConstraint.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. _secondViewAttribute = secondViewAttribute;
  100. } else {
  101. NSAssert(NO, @"attempting to add unsupported attribute: %@", secondViewAttribute);
  102. }
  103. }
  104. #pragma mark - NSLayoutConstraint multiplier proxies
  105. - (MASConstraint * (^)(CGFloat))multipliedBy {
  106. return ^id(CGFloat multiplier) {
  107. NSAssert(!self.hasBeenInstalled,
  108. @"Cannot modify constraint multiplier after it has been installed");
  109. self.layoutMultiplier = multiplier;
  110. return self;
  111. };
  112. }
  113. - (MASConstraint * (^)(CGFloat))dividedBy {
  114. return ^id(CGFloat divider) {
  115. NSAssert(!self.hasBeenInstalled,
  116. @"Cannot modify constraint multiplier after it has been installed");
  117. self.layoutMultiplier = 1.0/divider;
  118. return self;
  119. };
  120. }
  121. #pragma mark - MASLayoutPriority proxy
  122. - (MASConstraint * (^)(MASLayoutPriority))priority {
  123. return ^id(MASLayoutPriority priority) {
  124. NSAssert(!self.hasBeenInstalled,
  125. @"Cannot modify constraint priority after it has been installed");
  126. self.layoutPriority = priority;
  127. return self;
  128. };
  129. }
  130. #pragma mark - NSLayoutRelation proxy
  131. - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation {
  132. return ^id(id attribute, NSLayoutRelation relation) {
  133. if ([attribute isKindOfClass:NSArray.class]) {
  134. NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation");
  135. NSMutableArray *children = NSMutableArray.new;
  136. for (id attr in attribute) {
  137. MASViewConstraint *viewConstraint = [self copy];
  138. viewConstraint.layoutRelation = relation;
  139. viewConstraint.secondViewAttribute = attr;
  140. [children addObject:viewConstraint];
  141. }
  142. MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
  143. compositeConstraint.delegate = self.delegate;
  144. [self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint];
  145. return compositeConstraint;
  146. } else {
  147. NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation");
  148. self.layoutRelation = relation;
  149. self.secondViewAttribute = attribute;
  150. return self;
  151. }
  152. };
  153. }
  154. #pragma mark - Semantic properties
  155. - (MASConstraint *)with {
  156. return self;
  157. }
  158. - (MASConstraint *)and {
  159. return self;
  160. }
  161. #pragma mark - attribute chaining
  162. - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
  163. NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation");
  164. return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
  165. }
  166. #pragma mark - Animator proxy
  167. #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
  168. - (MASConstraint *)animator {
  169. self.useAnimator = YES;
  170. return self;
  171. }
  172. #endif
  173. #pragma mark - debug helpers
  174. - (MASConstraint * (^)(id))key {
  175. return ^id(id key) {
  176. self.mas_key = key;
  177. return self;
  178. };
  179. }
  180. #pragma mark - NSLayoutConstraint constant setters
  181. - (void)setInsets:(MASEdgeInsets)insets {
  182. NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
  183. switch (layoutAttribute) {
  184. case NSLayoutAttributeLeft:
  185. case NSLayoutAttributeLeading:
  186. self.layoutConstant = insets.left;
  187. break;
  188. case NSLayoutAttributeTop:
  189. self.layoutConstant = insets.top;
  190. break;
  191. case NSLayoutAttributeBottom:
  192. self.layoutConstant = -insets.bottom;
  193. break;
  194. case NSLayoutAttributeRight:
  195. case NSLayoutAttributeTrailing:
  196. self.layoutConstant = -insets.right;
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. - (void)setInset:(CGFloat)inset {
  203. [self setInsets:(MASEdgeInsets){.top = inset, .left = inset, .bottom = inset, .right = inset}];
  204. }
  205. - (void)setOffset:(CGFloat)offset {
  206. self.layoutConstant = offset;
  207. }
  208. - (void)setSizeOffset:(CGSize)sizeOffset {
  209. NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
  210. switch (layoutAttribute) {
  211. case NSLayoutAttributeWidth:
  212. self.layoutConstant = sizeOffset.width;
  213. break;
  214. case NSLayoutAttributeHeight:
  215. self.layoutConstant = sizeOffset.height;
  216. break;
  217. default:
  218. break;
  219. }
  220. }
  221. - (void)setCenterOffset:(CGPoint)centerOffset {
  222. NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
  223. switch (layoutAttribute) {
  224. case NSLayoutAttributeCenterX:
  225. self.layoutConstant = centerOffset.x;
  226. break;
  227. case NSLayoutAttributeCenterY:
  228. self.layoutConstant = centerOffset.y;
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. #pragma mark - MASConstraint
  235. - (void)activate {
  236. [self install];
  237. }
  238. - (void)deactivate {
  239. [self uninstall];
  240. }
  241. - (void)install {
  242. if (self.hasBeenInstalled) {
  243. return;
  244. }
  245. if ([self supportsActiveProperty] && self.layoutConstraint) {
  246. self.layoutConstraint.active = YES;
  247. [self.firstViewAttribute.view.mas_installedConstraints addObject:self];
  248. return;
  249. }
  250. MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item;
  251. NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute;
  252. MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item;
  253. NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute;
  254. // alignment attributes must have a secondViewAttribute
  255. // therefore we assume that is refering to superview
  256. // eg make.left.equalTo(@10)
  257. if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) {
  258. secondLayoutItem = self.firstViewAttribute.view.superview;
  259. secondLayoutAttribute = firstLayoutAttribute;
  260. }
  261. MASLayoutConstraint *layoutConstraint
  262. = [MASLayoutConstraint constraintWithItem:firstLayoutItem
  263. attribute:firstLayoutAttribute
  264. relatedBy:self.layoutRelation
  265. toItem:secondLayoutItem
  266. attribute:secondLayoutAttribute
  267. multiplier:self.layoutMultiplier
  268. constant:self.layoutConstant];
  269. layoutConstraint.priority = self.layoutPriority;
  270. layoutConstraint.mas_key = self.mas_key;
  271. if (self.secondViewAttribute.view) {
  272. MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view];
  273. NSAssert(closestCommonSuperview,
  274. @"couldn't find a common superview for %@ and %@",
  275. self.firstViewAttribute.view, self.secondViewAttribute.view);
  276. self.installedView = closestCommonSuperview;
  277. } else if (self.firstViewAttribute.isSizeAttribute) {
  278. self.installedView = self.firstViewAttribute.view;
  279. } else {
  280. self.installedView = self.firstViewAttribute.view.superview;
  281. }
  282. MASLayoutConstraint *existingConstraint = nil;
  283. if (self.updateExisting) {
  284. existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint];
  285. }
  286. if (existingConstraint) {
  287. // just update the constant
  288. existingConstraint.constant = layoutConstraint.constant;
  289. self.layoutConstraint = existingConstraint;
  290. } else {
  291. [self.installedView addConstraint:layoutConstraint];
  292. self.layoutConstraint = layoutConstraint;
  293. [firstLayoutItem.mas_installedConstraints addObject:self];
  294. }
  295. }
  296. - (MASLayoutConstraint *)layoutConstraintSimilarTo:(MASLayoutConstraint *)layoutConstraint {
  297. // check if any constraints are the same apart from the only mutable property constant
  298. // go through constraints in reverse as we do not want to match auto-resizing or interface builder constraints
  299. // and they are likely to be added first.
  300. for (NSLayoutConstraint *existingConstraint in self.installedView.constraints.reverseObjectEnumerator) {
  301. if (![existingConstraint isKindOfClass:MASLayoutConstraint.class]) continue;
  302. if (existingConstraint.firstItem != layoutConstraint.firstItem) continue;
  303. if (existingConstraint.secondItem != layoutConstraint.secondItem) continue;
  304. if (existingConstraint.firstAttribute != layoutConstraint.firstAttribute) continue;
  305. if (existingConstraint.secondAttribute != layoutConstraint.secondAttribute) continue;
  306. if (existingConstraint.relation != layoutConstraint.relation) continue;
  307. if (existingConstraint.multiplier != layoutConstraint.multiplier) continue;
  308. if (existingConstraint.priority != layoutConstraint.priority) continue;
  309. return (id)existingConstraint;
  310. }
  311. return nil;
  312. }
  313. - (void)uninstall {
  314. if ([self supportsActiveProperty]) {
  315. self.layoutConstraint.active = NO;
  316. [self.firstViewAttribute.view.mas_installedConstraints removeObject:self];
  317. return;
  318. }
  319. [self.installedView removeConstraint:self.layoutConstraint];
  320. self.layoutConstraint = nil;
  321. self.installedView = nil;
  322. [self.firstViewAttribute.view.mas_installedConstraints removeObject:self];
  323. }
  324. @end