PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/TableViewCellWithAutoLayout/PureLayout/ALView+PureLayout.m

https://gitlab.com/lisit1003/TableViewCellWithAutoLayoutiOS8
Objective C | 1061 lines | 514 code | 102 blank | 445 comment | 96 complexity | 56ff09c31b5ff25ef7e4d1b0455323bd MD5 | raw file
  1. //
  2. // ALView+PureLayout.m
  3. // v2.0.1
  4. // https://github.com/smileyborg/PureLayout
  5. //
  6. // Copyright (c) 2012 Richard Turton
  7. // Copyright (c) 2013-2014 Tyler Fox
  8. //
  9. // This code is distributed under the terms and conditions of the MIT license.
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to
  13. // deal in the Software without restriction, including without limitation the
  14. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  15. // sell copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  27. // IN THE SOFTWARE.
  28. //
  29. #import "ALView+PureLayout.h"
  30. #import "NSLayoutConstraint+PureLayout.h"
  31. #import "NSArray+PureLayout.h"
  32. #import "PureLayout+Internal.h"
  33. #pragma mark - ALView+PureLayout
  34. @implementation ALView (PureLayout)
  35. #pragma mark Factory & Initializer Methods
  36. /**
  37. Creates and returns a new view that does not convert the autoresizing mask into constraints.
  38. */
  39. + (instancetype)newAutoLayoutView
  40. {
  41. ALView *view = [self new];
  42. view.translatesAutoresizingMaskIntoConstraints = NO;
  43. return view;
  44. }
  45. /**
  46. Initializes and returns a new view that does not convert the autoresizing mask into constraints.
  47. */
  48. - (instancetype)initForAutoLayout
  49. {
  50. self = [self init];
  51. if (self) {
  52. self.translatesAutoresizingMaskIntoConstraints = NO;
  53. }
  54. return self;
  55. }
  56. #pragma mark Create Constraints Without Installing
  57. /**
  58. A global variable that is incremented when the constraints block passed in to the
  59. +[autoCreateConstraintsWithoutInstalling:] method begins executing, and decremented when the block
  60. finishes executing. As a result, this variable will be 0 when not inside a constraints block, and
  61. greater than 0 when inside a constraints block.
  62. NOTE: Access to this variable is not synchronized (and should only be done on the main thread).
  63. */
  64. static NSUInteger _al_preventAutomaticConstraintInstallationCount = 0;
  65. /**
  66. Accessor for the global state that determines whether automatic constraint installation should be prevented.
  67. */
  68. + (BOOL)al_preventAutomaticConstraintInstallation
  69. {
  70. return _al_preventAutomaticConstraintInstallationCount > 0;
  71. }
  72. /**
  73. Prevents constraints created in the given constraints block from being automatically installed (activated).
  74. The created constraints returned from each PureLayout API call must be stored, as they are not retained.
  75. @param block A block of method calls to the PureLayout API that create constraints.
  76. */
  77. + (void)autoCreateConstraintsWithoutInstalling:(ALConstraintsBlock)block
  78. {
  79. NSAssert(block, @"The constraints block cannot be nil.");
  80. if (block) {
  81. _al_preventAutomaticConstraintInstallationCount++;
  82. block();
  83. _al_preventAutomaticConstraintInstallationCount--;
  84. }
  85. }
  86. #pragma mark Set Priority For Constraints
  87. /**
  88. A global variable that stores a stack of layout priorities to set on constraints.
  89. When executing a constraints block passed into the +[autoSetPriority:forConstraints:] method, the priority for
  90. that call is pushed onto this stack, and when the block finishes executing, that priority is popped off this
  91. stack. If this stack contains at least 1 priority, the priority at the top of the stack will be set for all
  92. constraints created by this library (even if automatic constraint installation is being prevented).
  93. NOTE: Access to this variable is not synchronized (and should only be done on the main thread).
  94. */
  95. static NSMutableArray *_al_globalConstraintPriorities = nil;
  96. /**
  97. Accessor for the global stack of layout priorities.
  98. */
  99. + (NSMutableArray *)al_globalConstraintPriorities
  100. {
  101. if (!_al_globalConstraintPriorities) {
  102. _al_globalConstraintPriorities = [NSMutableArray new];
  103. }
  104. return _al_globalConstraintPriorities;
  105. }
  106. /**
  107. Returns the current layout priority to use for constraints.
  108. When executing a constraints block passed into +[autoSetPriority:forConstraints:], this will return
  109. the priority for the current block. Otherwise, the default Required priority is returned.
  110. */
  111. + (ALLayoutPriority)al_currentGlobalConstraintPriority
  112. {
  113. NSMutableArray *globalConstraintPriorities = [self al_globalConstraintPriorities];
  114. if ([globalConstraintPriorities count] == 0) {
  115. return ALLayoutPriorityRequired;
  116. }
  117. return [[globalConstraintPriorities lastObject] floatValue];
  118. }
  119. /**
  120. Accessor for the global state that determines if we're currently in the scope of a priority constraints block.
  121. */
  122. + (BOOL)al_isExecutingPriorityConstraintsBlock
  123. {
  124. return [[ALView al_globalConstraintPriorities] count] > 0;
  125. }
  126. /**
  127. Sets the constraint priority to the given value for all constraints created using the PureLayout
  128. API within the given constraints block.
  129. NOTE: This method will have no effect (and will NOT set the priority) on constraints created or added
  130. without using the PureLayout API!
  131. @param priority The layout priority to be set on all constraints created in the constraints block.
  132. @param block A block of method calls to the PureLayout API that create and install constraints.
  133. */
  134. + (void)autoSetPriority:(ALLayoutPriority)priority forConstraints:(ALConstraintsBlock)block
  135. {
  136. NSAssert(block, @"The constraints block cannot be nil.");
  137. if (block) {
  138. [[ALView al_globalConstraintPriorities] addObject:@(priority)];
  139. block();
  140. [[ALView al_globalConstraintPriorities] removeLastObject];
  141. }
  142. }
  143. #pragma mark Set Identifier For Constraints
  144. #if __PureLayout_MinBaseSDK_iOS_8_0
  145. /**
  146. A global variable that stores a stack of identifier strings to set on constraints.
  147. When executing a constraints block passed into the +[autoSetIdentifier:forConstraints:] method, the identifier for
  148. that call is pushed onto this stack, and when the block finishes executing, that identifier is popped off this
  149. stack. If this stack contains at least 1 identifier, the identifier at the top of the stack will be set for all
  150. constraints created by this library (even if automatic constraint installation is being prevented).
  151. NOTE: Access to this variable is not synchronized (and should only be done on the main thread).
  152. */
  153. static NSMutableArray *_al_globalConstraintIdentifiers = nil;
  154. /**
  155. Accessor for the global state of constraint identifiers.
  156. */
  157. + (NSMutableArray *)al_globalConstraintIdentifiers
  158. {
  159. if (!_al_globalConstraintIdentifiers) {
  160. _al_globalConstraintIdentifiers = [NSMutableArray new];
  161. }
  162. return _al_globalConstraintIdentifiers;
  163. }
  164. /**
  165. Returns the current identifier string to use for constraints.
  166. When executing a constraints block passed into +[autoSetIdentifier:forConstraints:], this will return
  167. the identifier for the current block. Otherwise, nil is returned.
  168. */
  169. + (NSString *)al_currentGlobalConstraintIdentifier
  170. {
  171. NSMutableArray *globalConstraintIdentifiers = [self al_globalConstraintIdentifiers];
  172. if ([globalConstraintIdentifiers count] == 0) {
  173. return nil;
  174. }
  175. return [globalConstraintIdentifiers lastObject];
  176. }
  177. /**
  178. Sets the identifier for all constraints created using the PureLayout API within the given constraints block.
  179. NOTE: This method will have no effect (and will NOT set the identifier) on constraints created or added
  180. without using the PureLayout API!
  181. @param identifer A string used to identify all constraints created in the constraints block.
  182. @param block A block of method calls to the PureLayout API that create and install constraints.
  183. */
  184. + (void)autoSetIdentifier:(NSString *)identifer forConstraints:(ALConstraintsBlock)block
  185. {
  186. NSAssert(block, @"The constraints block cannot be nil.");
  187. NSAssert(identifer, @"The identifier string cannot be nil.");
  188. if (block) {
  189. if (identifer) {
  190. [[ALView al_globalConstraintIdentifiers] addObject:identifer];
  191. }
  192. block();
  193. if (identifer) {
  194. [[ALView al_globalConstraintIdentifiers] removeLastObject];
  195. }
  196. }
  197. }
  198. #endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
  199. #pragma mark Center in Superview
  200. /**
  201. Centers the view in its superview.
  202. @return An array of constraints added.
  203. */
  204. - (NSArray *)autoCenterInSuperview
  205. {
  206. NSMutableArray *constraints = [NSMutableArray new];
  207. [constraints addObject:[self autoAlignAxisToSuperviewAxis:ALAxisHorizontal]];
  208. [constraints addObject:[self autoAlignAxisToSuperviewAxis:ALAxisVertical]];
  209. return constraints;
  210. }
  211. /**
  212. Aligns the view to the same axis of its superview.
  213. @param axis The axis of this view and of its superview to align.
  214. @return The constraint added.
  215. */
  216. - (NSLayoutConstraint *)autoAlignAxisToSuperviewAxis:(ALAxis)axis
  217. {
  218. self.translatesAutoresizingMaskIntoConstraints = NO;
  219. ALView *superview = self.superview;
  220. NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
  221. return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)axis ofView:superview];
  222. }
  223. #if __PureLayout_MinBaseSDK_iOS_8_0
  224. /**
  225. Centers the view in its superview, taking into account the layout margins of both the view and its superview.
  226. @return An array of constraints added.
  227. */
  228. - (NSArray *)autoCenterInSuperviewMargins
  229. {
  230. NSMutableArray *constraints = [NSMutableArray new];
  231. [constraints addObject:[self autoAlignAxisToSuperviewMarginAxis:ALAxisHorizontal]];
  232. [constraints addObject:[self autoAlignAxisToSuperviewMarginAxis:ALAxisVertical]];
  233. return constraints;
  234. }
  235. /**
  236. Aligns the view to the corresponding margin axis of its superview.
  237. @param axis The axis of this view to align to the corresponding margin axis of its superview.
  238. @return The constraint added.
  239. */
  240. - (NSLayoutConstraint *)autoAlignAxisToSuperviewMarginAxis:(ALAxis)axis
  241. {
  242. self.translatesAutoresizingMaskIntoConstraints = NO;
  243. ALView *superview = self.superview;
  244. NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
  245. ALMarginAxis marginAxis = [NSLayoutConstraint al_marginAxisForAxis:axis];
  246. return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)marginAxis ofView:superview];
  247. }
  248. #endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
  249. #pragma mark Pin Edges to Superview
  250. /**
  251. Pins the given edge of the view to the same edge of its superview.
  252. @param edge The edge of this view and its superview to pin.
  253. @return The constraint added.
  254. */
  255. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge
  256. {
  257. return [self autoPinEdgeToSuperviewEdge:edge withInset:0.0];
  258. }
  259. /**
  260. Pins the given edge of the view to the same edge of its superview with an inset.
  261. @param edge The edge of this view and its superview to pin.
  262. @param inset The amount to inset this view's edge from the superview's edge.
  263. @return The constraint added.
  264. */
  265. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset
  266. {
  267. return [self autoPinEdgeToSuperviewEdge:edge withInset:inset relation:NSLayoutRelationEqual];
  268. }
  269. /**
  270. Pins the given edge of the view to the same edge of its superview with an inset as a maximum or minimum.
  271. @param edge The edge of this view and its superview to pin.
  272. @param inset The amount to inset this view's edge from the superview's edge.
  273. @param relation Whether the inset should be at least, at most, or exactly equal to the given value.
  274. @return The constraint added.
  275. */
  276. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset relation:(NSLayoutRelation)relation
  277. {
  278. self.translatesAutoresizingMaskIntoConstraints = NO;
  279. ALView *superview = self.superview;
  280. NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
  281. if (edge == ALEdgeBottom || edge == ALEdgeRight || edge == ALEdgeTrailing) {
  282. // The bottom, right, and trailing insets (and relations, if an inequality) are inverted to become offsets
  283. inset = -inset;
  284. if (relation == NSLayoutRelationLessThanOrEqual) {
  285. relation = NSLayoutRelationGreaterThanOrEqual;
  286. } else if (relation == NSLayoutRelationGreaterThanOrEqual) {
  287. relation = NSLayoutRelationLessThanOrEqual;
  288. }
  289. }
  290. return [self autoPinEdge:edge toEdge:edge ofView:superview withOffset:inset relation:relation];
  291. }
  292. /**
  293. Pins the edges of the view to the edges of its superview with the given edge insets.
  294. The insets.left corresponds to a leading edge constraint, and insets.right corresponds to a trailing edge constraint.
  295. @param insets The insets for this view's edges from its superview's edges.
  296. @return An array of constraints added.
  297. */
  298. - (NSArray *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets
  299. {
  300. NSMutableArray *constraints = [NSMutableArray new];
  301. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:insets.top]];
  302. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:insets.left]];
  303. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:insets.bottom]];
  304. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:insets.right]];
  305. return constraints;
  306. }
  307. /**
  308. Pins 3 of the 4 edges of the view to the edges of its superview with the given edge insets, excluding one edge.
  309. The insets.left corresponds to a leading edge constraint, and insets.right corresponds to a trailing edge constraint.
  310. @param insets The insets for this view's edges from its superview's edges. The inset corresponding to the excluded edge
  311. will be ignored.
  312. @param edge The edge of this view to exclude in pinning to its superview; this method will not apply any constraint to it.
  313. @return An array of constraints added.
  314. */
  315. - (NSArray *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets excludingEdge:(ALEdge)edge
  316. {
  317. NSMutableArray *constraints = [NSMutableArray new];
  318. if (edge != ALEdgeTop) {
  319. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:insets.top]];
  320. }
  321. if (edge != ALEdgeLeading && edge != ALEdgeLeft) {
  322. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:insets.left]];
  323. }
  324. if (edge != ALEdgeBottom) {
  325. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:insets.bottom]];
  326. }
  327. if (edge != ALEdgeTrailing && edge != ALEdgeRight) {
  328. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:insets.right]];
  329. }
  330. return constraints;
  331. }
  332. #if __PureLayout_MinBaseSDK_iOS_8_0
  333. /**
  334. Pins the given edge of the view to the corresponding margin of its superview.
  335. @param edge The edge of this view to pin to the corresponding margin of its superview.
  336. @return The constraint added.
  337. */
  338. - (NSLayoutConstraint *)autoPinEdgeToSuperviewMargin:(ALEdge)edge
  339. {
  340. return [self autoPinEdgeToSuperviewMargin:edge relation:NSLayoutRelationEqual];
  341. }
  342. /**
  343. Pins the given edge of the view to the corresponding margin of its superview as a maximum or minimum.
  344. @param edge The edge of this view to pin to the corresponding margin of its superview.
  345. @param relation Whether the edge should be inset by at least, at most, or exactly the superview's margin.
  346. @return The constraint added.
  347. */
  348. - (NSLayoutConstraint *)autoPinEdgeToSuperviewMargin:(ALEdge)edge relation:(NSLayoutRelation)relation
  349. {
  350. self.translatesAutoresizingMaskIntoConstraints = NO;
  351. ALView *superview = self.superview;
  352. NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
  353. if (edge == ALEdgeBottom || edge == ALEdgeRight || edge == ALEdgeTrailing) {
  354. // The bottom, right, and trailing relations are inverted
  355. if (relation == NSLayoutRelationLessThanOrEqual) {
  356. relation = NSLayoutRelationGreaterThanOrEqual;
  357. } else if (relation == NSLayoutRelationGreaterThanOrEqual) {
  358. relation = NSLayoutRelationLessThanOrEqual;
  359. }
  360. }
  361. ALMargin margin = [NSLayoutConstraint al_marginForEdge:edge];
  362. return [self autoConstrainAttribute:(ALAttribute)edge toAttribute:(ALAttribute)margin ofView:superview withOffset:0.0 relation:relation];
  363. }
  364. /**
  365. Pins the edges of the view to the margins of its superview.
  366. @return An array of constraints added.
  367. */
  368. - (NSArray *)autoPinEdgesToSuperviewMargins
  369. {
  370. NSMutableArray *constraints = [NSMutableArray new];
  371. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeTop]];
  372. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeLeading]];
  373. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeBottom]];
  374. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeTrailing]];
  375. return constraints;
  376. }
  377. /**
  378. Pins 3 of the 4 edges of the view to the margins of its superview, excluding one edge.
  379. @param edge The edge of this view to exclude in pinning to its superview; this method will not apply any constraint to it.
  380. @return An array of constraints added.
  381. */
  382. - (NSArray *)autoPinEdgesToSuperviewMarginsExcludingEdge:(ALEdge)edge
  383. {
  384. NSMutableArray *constraints = [NSMutableArray new];
  385. if (edge != ALEdgeTop) {
  386. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeTop]];
  387. }
  388. if (edge != ALEdgeLeading && edge != ALEdgeLeft) {
  389. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeLeading]];
  390. }
  391. if (edge != ALEdgeBottom) {
  392. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeBottom]];
  393. }
  394. if (edge != ALEdgeTrailing && edge != ALEdgeRight) {
  395. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeTrailing]];
  396. }
  397. return constraints;
  398. }
  399. #endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
  400. #pragma mark Pin Edges
  401. /**
  402. Pins an edge of the view to a given edge of another view.
  403. @param edge The edge of this view to pin.
  404. @param toEdge The edge of the other view to pin to.
  405. @param otherView The other view to pin to. Must be in the same view hierarchy as this view.
  406. @return The constraint added.
  407. */
  408. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView
  409. {
  410. return [self autoPinEdge:edge toEdge:toEdge ofView:otherView withOffset:0.0];
  411. }
  412. /**
  413. Pins an edge of the view to a given edge of another view with an offset.
  414. @param edge The edge of this view to pin.
  415. @param toEdge The edge of the other view to pin to.
  416. @param otherView The other view to pin to. Must be in the same view hierarchy as this view.
  417. @param offset The offset between the edge of this view and the edge of the other view.
  418. @return The constraint added.
  419. */
  420. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView withOffset:(CGFloat)offset
  421. {
  422. return [self autoPinEdge:edge toEdge:toEdge ofView:otherView withOffset:offset relation:NSLayoutRelationEqual];
  423. }
  424. /**
  425. Pins an edge of the view to a given edge of another view with an offset as a maximum or minimum.
  426. @param edge The edge of this view to pin.
  427. @param toEdge The edge of the other view to pin to.
  428. @param otherView The other view to pin to. Must be in the same view hierarchy as this view.
  429. @param offset The offset between the edge of this view and the edge of the other view.
  430. @param relation Whether the offset should be at least, at most, or exactly equal to the given value.
  431. @return The constraint added.
  432. */
  433. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation
  434. {
  435. return [self autoConstrainAttribute:(ALAttribute)edge toAttribute:(ALAttribute)toEdge ofView:otherView withOffset:offset relation:relation];
  436. }
  437. #pragma mark Align Axes
  438. /**
  439. Aligns an axis of the view to the same axis of another view.
  440. @param axis The axis of this view and the other view to align.
  441. @param otherView The other view to align to. Must be in the same view hierarchy as this view.
  442. @return The constraint added.
  443. */
  444. - (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView
  445. {
  446. return [self autoAlignAxis:axis toSameAxisOfView:otherView withOffset:0.0];
  447. }
  448. /**
  449. Aligns an axis of the view to the same axis of another view with an offset.
  450. @param axis The axis of this view and the other view to align.
  451. @param otherView The other view to align to. Must be in the same view hierarchy as this view.
  452. @param offset The offset between the axis of this view and the axis of the other view.
  453. @return The constraint added.
  454. */
  455. - (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView withOffset:(CGFloat)offset
  456. {
  457. return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)axis ofView:otherView withOffset:offset];
  458. }
  459. #pragma mark Match Dimensions
  460. /**
  461. Matches a dimension of the view to a given dimension of another view.
  462. @param dimension The dimension of this view to pin.
  463. @param toDimension The dimension of the other view to pin to.
  464. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  465. @return The constraint added.
  466. */
  467. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView
  468. {
  469. return [self autoMatchDimension:dimension toDimension:toDimension ofView:otherView withOffset:0.0];
  470. }
  471. /**
  472. Matches a dimension of the view to a given dimension of another view with an offset.
  473. @param dimension The dimension of this view to pin.
  474. @param toDimension The dimension of the other view to pin to.
  475. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  476. @param offset The offset between the dimension of this view and the dimension of the other view.
  477. @return The constraint added.
  478. */
  479. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withOffset:(CGFloat)offset
  480. {
  481. return [self autoMatchDimension:dimension toDimension:toDimension ofView:otherView withOffset:offset relation:NSLayoutRelationEqual];
  482. }
  483. /**
  484. Matches a dimension of the view to a given dimension of another view with an offset as a maximum or minimum.
  485. @param dimension The dimension of this view to pin.
  486. @param toDimension The dimension of the other view to pin to.
  487. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  488. @param offset The offset between the dimension of this view and the dimension of the other view.
  489. @param relation Whether the offset should be at least, at most, or exactly equal to the given value.
  490. @return The constraint added.
  491. */
  492. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation
  493. {
  494. return [self autoConstrainAttribute:(ALAttribute)dimension toAttribute:(ALAttribute)toDimension ofView:otherView withOffset:offset relation:relation];
  495. }
  496. /**
  497. Matches a dimension of the view to a multiple of a given dimension of another view.
  498. @param dimension The dimension of this view to pin.
  499. @param toDimension The dimension of the other view to pin to.
  500. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  501. @param multiplier The multiple of the other view's given dimension that this view's given dimension should be.
  502. @return The constraint added.
  503. */
  504. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier
  505. {
  506. return [self autoMatchDimension:dimension toDimension:toDimension ofView:otherView withMultiplier:multiplier relation:NSLayoutRelationEqual];
  507. }
  508. /**
  509. Matches a dimension of the view to a multiple of a given dimension of another view as a maximum or minimum.
  510. @param dimension The dimension of this view to pin.
  511. @param toDimension The dimension of the other view to pin to.
  512. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  513. @param multiplier The multiple of the other view's given dimension that this view's given dimension should be.
  514. @param relation Whether the multiple should be at least, at most, or exactly equal to the given value.
  515. @return The constraint added.
  516. */
  517. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation
  518. {
  519. return [self autoConstrainAttribute:(ALAttribute)dimension toAttribute:(ALAttribute)toDimension ofView:otherView withMultiplier:multiplier relation:relation];
  520. }
  521. #pragma mark Set Dimensions
  522. /**
  523. Sets the view to a specific size.
  524. @param size The size to set this view's dimensions to.
  525. @return An array of constraints added.
  526. */
  527. - (NSArray *)autoSetDimensionsToSize:(CGSize)size
  528. {
  529. NSMutableArray *constraints = [NSMutableArray new];
  530. [constraints addObject:[self autoSetDimension:ALDimensionWidth toSize:size.width]];
  531. [constraints addObject:[self autoSetDimension:ALDimensionHeight toSize:size.height]];
  532. return constraints;
  533. }
  534. /**
  535. Sets the given dimension of the view to a specific size.
  536. @param dimension The dimension of this view to set.
  537. @param size The size to set the given dimension to.
  538. @return The constraint added.
  539. */
  540. - (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size
  541. {
  542. return [self autoSetDimension:dimension toSize:size relation:NSLayoutRelationEqual];
  543. }
  544. /**
  545. Sets the given dimension of the view to a specific size as a maximum or minimum.
  546. @param dimension The dimension of this view to set.
  547. @param size The size to set the given dimension to.
  548. @param relation Whether the size should be at least, at most, or exactly equal to the given value.
  549. @return The constraint added.
  550. */
  551. - (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size relation:(NSLayoutRelation)relation
  552. {
  553. self.translatesAutoresizingMaskIntoConstraints = NO;
  554. NSLayoutAttribute layoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:(ALAttribute)dimension];
  555. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:layoutAttribute relatedBy:relation toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:size];
  556. [constraint autoInstall];
  557. return constraint;
  558. }
  559. #pragma mark Set Content Compression Resistance & Hugging
  560. /**
  561. Sets the priority of content compression resistance for an axis.
  562. NOTE: This method must be called from within the block passed into the method +[autoSetPriority:forConstraints:]
  563. @param axis The axis to set the content compression resistance priority for.
  564. */
  565. - (void)autoSetContentCompressionResistancePriorityForAxis:(ALAxis)axis
  566. {
  567. NSAssert([ALView al_isExecutingPriorityConstraintsBlock], @"%@ should only be called from within the block passed into the method +[autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
  568. if ([ALView al_isExecutingPriorityConstraintsBlock]) {
  569. self.translatesAutoresizingMaskIntoConstraints = NO;
  570. ALLayoutConstraintAxis constraintAxis = [NSLayoutConstraint al_constraintAxisForAxis:axis];
  571. #if TARGET_OS_IPHONE
  572. [self setContentCompressionResistancePriority:[ALView al_currentGlobalConstraintPriority] forAxis:constraintAxis];
  573. #else
  574. [self setContentCompressionResistancePriority:[ALView al_currentGlobalConstraintPriority] forOrientation:constraintAxis];
  575. #endif /* TARGET_OS_IPHONE */
  576. }
  577. }
  578. /**
  579. Sets the priority of content hugging for an axis.
  580. NOTE: This method must be called from within the block passed into the method +[autoSetPriority:forConstraints:]
  581. @param axis The axis to set the content hugging priority for.
  582. */
  583. - (void)autoSetContentHuggingPriorityForAxis:(ALAxis)axis
  584. {
  585. NSAssert([ALView al_isExecutingPriorityConstraintsBlock], @"%@ should only be called from within the block passed into the method +[autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
  586. if ([ALView al_isExecutingPriorityConstraintsBlock]) {
  587. self.translatesAutoresizingMaskIntoConstraints = NO;
  588. ALLayoutConstraintAxis constraintAxis = [NSLayoutConstraint al_constraintAxisForAxis:axis];
  589. #if TARGET_OS_IPHONE
  590. [self setContentHuggingPriority:[ALView al_currentGlobalConstraintPriority] forAxis:constraintAxis];
  591. #else
  592. [self setContentHuggingPriority:[ALView al_currentGlobalConstraintPriority] forOrientation:constraintAxis];
  593. #endif /* TARGET_OS_IPHONE */
  594. }
  595. }
  596. #pragma mark Constrain Any Attributes
  597. /**
  598. Constrains an attribute of the view to a given attribute of another view.
  599. This method can be used to constrain different types of attributes across two views.
  600. @param attribute Any attribute of this view to constrain.
  601. @param toAttribute Any attribute of the other view to constrain to.
  602. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  603. @return The constraint added.
  604. */
  605. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView
  606. {
  607. return [self autoConstrainAttribute:attribute toAttribute:toAttribute ofView:otherView withOffset:0.0];
  608. }
  609. /**
  610. Constrains an attribute of the view to a given attribute of another view with an offset.
  611. This method can be used to constrain different types of attributes across two views.
  612. @param attribute Any attribute of this view to constrain.
  613. @param toAttribute Any attribute of the other view to constrain to.
  614. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  615. @param offset The offset between the attribute of this view and the attribute of the other view.
  616. @return The constraint added.
  617. */
  618. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withOffset:(CGFloat)offset
  619. {
  620. return [self autoConstrainAttribute:attribute toAttribute:toAttribute ofView:otherView withOffset:offset relation:NSLayoutRelationEqual];
  621. }
  622. /**
  623. Constrains an attribute of the view to a given attribute of another view with an offset as a maximum or minimum.
  624. This method can be used to constrain different types of attributes across two views.
  625. @param attribute Any attribute of this view to constrain.
  626. @param toAttribute Any attribute of the other view to constrain to.
  627. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  628. @param offset The offset between the attribute of this view and the attribute of the other view.
  629. @param relation Whether the offset should be at least, at most, or exactly equal to the given value.
  630. @return The constraint added.
  631. */
  632. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation
  633. {
  634. self.translatesAutoresizingMaskIntoConstraints = NO;
  635. NSLayoutAttribute layoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:attribute];
  636. NSLayoutAttribute toLayoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:toAttribute];
  637. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:layoutAttribute relatedBy:relation toItem:otherView attribute:toLayoutAttribute multiplier:1.0 constant:offset];
  638. [constraint autoInstall];
  639. return constraint;
  640. }
  641. /**
  642. Constrains an attribute of the view to a given attribute of another view with a multiplier.
  643. This method can be used to constrain different types of attributes across two views.
  644. @param attribute Any attribute of this view to constrain.
  645. @param toAttribute Any attribute of the other view to constrain to.
  646. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  647. @param multiplier The multiplier between the attribute of this view and the attribute of the other view.
  648. @return The constraint added.
  649. */
  650. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier
  651. {
  652. return [self autoConstrainAttribute:attribute toAttribute:toAttribute ofView:otherView withMultiplier:multiplier relation:NSLayoutRelationEqual];
  653. }
  654. /**
  655. Constrains an attribute of the view to a given attribute of another view with a multiplier as a maximum or minimum.
  656. This method can be used to constrain different types of attributes across two views.
  657. @param attribute Any attribute of this view to constrain.
  658. @param toAttribute Any attribute of the other view to constrain to.
  659. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  660. @param multiplier The multiplier between the attribute of this view and the attribute of the other view.
  661. @param relation Whether the multiplier should be at least, at most, or exactly equal to the given value.
  662. @return The constraint added.
  663. */
  664. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation
  665. {
  666. self.translatesAutoresizingMaskIntoConstraints = NO;
  667. NSLayoutAttribute layoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:attribute];
  668. NSLayoutAttribute toLayoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:toAttribute];
  669. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:layoutAttribute relatedBy:relation toItem:otherView attribute:toLayoutAttribute multiplier:multiplier constant:0.0];
  670. [constraint autoInstall];
  671. return constraint;
  672. }
  673. #pragma mark Pin to Layout Guides
  674. #if TARGET_OS_IPHONE
  675. /**
  676. Pins the top edge of the view to the top layout guide of the given view controller with an inset.
  677. For compatibility with iOS 6 (where layout guides do not exist), this method will simply pin the top edge of
  678. the view to the top edge of the given view controller's view with an inset.
  679. @param viewController The view controller whose topLayoutGuide should be used to pin to.
  680. @param inset The amount to inset this view's top edge from the layout guide.
  681. @return The constraint added.
  682. */
  683. - (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset
  684. {
  685. return [self autoPinToTopLayoutGuideOfViewController:viewController withInset:inset relation:NSLayoutRelationEqual];
  686. }
  687. - (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset relation:(NSLayoutRelation)relation
  688. {
  689. if (__PureLayout_MinSysVer_iOS_7_0) {
  690. self.translatesAutoresizingMaskIntoConstraints = NO;
  691. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:relation toItem:viewController.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:inset];
  692. [viewController.view al_addConstraint:constraint]; // Can't use autoInstall because the layout guide is not a view
  693. return constraint;
  694. } else {
  695. // iOS 6 fallback
  696. return [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:viewController.view withOffset:inset relation:relation];
  697. }
  698. }
  699. /**
  700. Pins the bottom edge of the view to the bottom layout guide of the given view controller with an inset.
  701. For compatibility with iOS 6 (where layout guides do not exist), this method will simply pin the bottom edge of
  702. the view to the bottom edge of the given view controller's view with an inset.
  703. @param viewController The view controller whose bottomLayoutGuide should be used to pin to.
  704. @param inset The amount to inset this view's bottom edge from the layout guide.
  705. @return The constraint added.
  706. */
  707. - (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset
  708. {
  709. return [self autoPinToBottomLayoutGuideOfViewController:viewController withInset:inset relation:NSLayoutRelationEqual];
  710. }
  711. - (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset relation:(NSLayoutRelation)relation
  712. {
  713. // The bottom inset (and relation, if an inequality) is inverted to become an offset
  714. inset = -inset;
  715. if (relation == NSLayoutRelationLessThanOrEqual) {
  716. relation = NSLayoutRelationGreaterThanOrEqual;
  717. } else if (relation == NSLayoutRelationGreaterThanOrEqual) {
  718. relation = NSLayoutRelationLessThanOrEqual;
  719. }
  720. if (__PureLayout_MinSysVer_iOS_7_0) {
  721. self.translatesAutoresizingMaskIntoConstraints = NO;
  722. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:relation toItem:viewController.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:inset];
  723. [viewController.view al_addConstraint:constraint]; // Can't use autoInstall because the layout guide is not a view
  724. return constraint;
  725. } else {
  726. // iOS 6 fallback
  727. return [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:viewController.view withOffset:inset relation:relation];
  728. }
  729. }
  730. #endif /* TARGET_OS_IPHONE */
  731. #pragma mark Deprecated Methods
  732. /**
  733. DEPRECATED as of PureLayout v2.0.0. Retain a reference to and remove specific constraints instead, or recreate the view(s) entirely to remove all constraints.
  734. Removes all explicit constraints that affect the view.
  735. WARNING: Apple's constraint solver is not optimized for large-scale constraint removal; you may encounter major performance issues after using this method.
  736. It is not recommended to use this method to "reset" a view for reuse in a different way with new constraints. Create a new view instead.
  737. NOTE: This method preserves implicit constraints, such as intrinsic content size constraints, which you usually do not want to remove.
  738. */
  739. - (void)autoRemoveConstraintsAffectingView
  740. {
  741. [self autoRemoveConstraintsAffectingViewIncludingImplicitConstraints:NO];
  742. }
  743. /**
  744. DEPRECATED as of PureLayout v2.0.0. Retain a reference to and remove specific constraints instead, or recreate the view(s) entirely to remove all constraints.
  745. Removes all constraints that affect the view, optionally including implicit constraints.
  746. WARNING: Apple's constraint solver is not optimized for large-scale constraint removal; you may encounter major performance issues after using this method.
  747. It is not recommended to use this method to "reset" a view for reuse in a different way with new constraints. Create a new view instead.
  748. NOTE: Implicit constraints are auto-generated lower priority constraints (such as those that attempt to keep a view at
  749. its intrinsic content size by hugging its content & resisting compression), and you usually do not want to remove these.
  750. @param shouldRemoveImplicitConstraints Whether implicit constraints should be removed or skipped.
  751. */
  752. - (void)autoRemoveConstraintsAffectingViewIncludingImplicitConstraints:(BOOL)shouldRemoveImplicitConstraints
  753. {
  754. NSMutableArray *constraintsToRemove = [NSMutableArray new];
  755. ALView *startView = self;
  756. do {
  757. for (NSLayoutConstraint *constraint in startView.constraints) {
  758. BOOL isImplicitConstraint = [NSStringFromClass([constraint class]) isEqualToString:@"NSContentSizeLayoutConstraint"];
  759. if (shouldRemoveImplicitConstraints || !isImplicitConstraint) {
  760. if (constraint.firstItem == self || constraint.secondItem == self) {
  761. [constraintsToRemove addObject:constraint];
  762. }
  763. }
  764. }
  765. startView = startView.superview;
  766. } while (startView);
  767. [constraintsToRemove autoRemoveConstraints];
  768. }
  769. /**
  770. DEPRECATED as of PureLayout v2.0.0. Retain a reference to and remove specific constraints instead, or recreate the view(s) entirely to remove all constraints.
  771. Recursively removes all explicit constraints that affect the view and its subviews.
  772. WARNING: Apple's constraint solver is not optimized for large-scale constraint removal; you may encounter major performance issues after using this method.
  773. It is not recommended to use this method to "reset" views for reuse in a different way with new constraints. Create a new view instead.
  774. NOTE: This method preserves implicit constraints, such as intrinsic content size constraints, which you usually do not want to remove.
  775. */
  776. - (void)autoRemoveConstraintsAffectingViewAndSubviews
  777. {
  778. [self autoRemoveConstraintsAffectingViewAndSubviewsIncludingImplicitConstraints:NO];
  779. }
  780. /**
  781. DEPRECATED as of PureLayout v2.0.0. Retain a reference to and remove specific constraints instead, or recreate the view(s) entirely to remove all constraints.
  782. Recursively removes all constraints that affect the view and its subviews, optionally including implicit constraints.
  783. WARNING: Apple's constraint solver is not optimized for large-scale constraint removal; you may encounter major performance issues after using this method.
  784. It is not recommended to use this method to "reset" views for reuse in a different way with new constraints. Create a new view instead.
  785. NOTE: Implicit constraints are auto-generated lower priority constraints (such as those that attempt to keep a view at
  786. its intrinsic content size by hugging its content & resisting compression), and you usually do not want to remove these.
  787. @param shouldRemoveImplicitConstraints Whether implicit constraints should be removed or skipped.
  788. */
  789. - (void)autoRemoveConstraintsAffectingViewAndSubviewsIncludingImplicitConstraints:(BOOL)shouldRemoveImplicitConstraints
  790. {
  791. [self autoRemoveConstraintsAffectingViewIncludingImplicitConstraints:shouldRemoveImplicitConstraints];
  792. for (ALView *subview in self.subviews) {
  793. [subview autoRemoveConstraintsAffectingViewAndSubviewsIncludingImplicitConstraints:shouldRemoveImplicitConstraints];
  794. }
  795. }
  796. #pragma mark Internal Methods
  797. /**
  798. Applies the global constraint priority and identifier to the given constraint.
  799. This should be done before installing all constraints.
  800. @param constraint The constraint to set the global priority and identifier on.
  801. */
  802. + (void)al_applyGlobalStateToConstraint:(NSLayoutConstraint *)constraint
  803. {
  804. if ([ALView al_isExecutingPriorityConstraintsBlock]) {
  805. constraint.priority = [ALView al_currentGlobalConstraintPriority];
  806. }
  807. #if __PureLayout_MinBaseSDK_iOS_8_0
  808. NSString *globalConstraintIdentifier = [ALView al_currentGlobalConstraintIdentifier];
  809. if (globalConstraintIdentifier) {
  810. [constraint autoIdentify:globalConstraintIdentifier];
  811. }
  812. #endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
  813. }
  814. /**
  815. Adds the given constraint to this view after applying the global state to the constraint.
  816. NOTE: This method is compatible with all versions of iOS, and should be used for older versions before the active
  817. property on NSLayoutConstraint was introduced.
  818. This method should be the only one that calls the UIView/NSView addConstraint: method directly.
  819. @param constraint The constraint to set the global priority on and then add to this view.
  820. */
  821. - (void)al_addConstraint:(NSLayoutConstraint *)constraint
  822. {
  823. [ALView al_applyGlobalStateToConstraint:constraint];
  824. if (![ALView al_preventAutomaticConstraintInstallation]) {
  825. [self addConstraint:constraint];
  826. }
  827. }
  828. /**
  829. Returns the common superview for this view and the given other view.
  830. Raises an exception if this view and the other view do not share a common superview.
  831. @return The common superview for the two views.
  832. */
  833. - (ALView *)al_commonSuperviewWithView:(ALView *)otherView
  834. {
  835. ALView *commonSuperview = nil;
  836. ALView *startView = self;
  837. do {
  838. #if TARGET_OS_IPHONE
  839. if ([otherView isDescendantOfView:startView]) {
  840. commonSuperview = startView;
  841. }
  842. #else
  843. if ([otherView isDescendantOf:startView]) {
  844. commonSuperview = startView;
  845. }
  846. #endif /* TARGET_OS_IPHONE */
  847. startView = startView.superview;
  848. } while (startView && !commonSuperview);
  849. NSAssert(commonSuperview, @"Can't constrain two views that do not share a common superview. Make sure that both views have been added into the same view hierarchy.");
  850. return commonSuperview;
  851. }
  852. /**
  853. Aligns this view to another view with an alignment attribute.
  854. @param attribute The attribute to use to align the two views.
  855. @param otherView The other view to align to.
  856. @param axis The axis along which the views are distributed, used to validate the alignment attribute.
  857. @return The constraint added.
  858. */
  859. - (NSLayoutConstraint *)al_alignAttribute:(ALAttribute)attribute toView:(ALView *)otherView forAxis:(ALAxis)axis
  860. {
  861. NSLayoutConstraint *constraint = nil;
  862. switch (attribute) {
  863. case ALAttributeVertical:
  864. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeVertical.");
  865. constraint = [self autoAlignAxis:ALAxisVertical toSameAxisOfView:otherView];
  866. break;
  867. case ALAttributeHorizontal:
  868. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeHorizontal.");
  869. constraint = [self autoAlignAxis:ALAxisHorizontal toSameAxisOfView:otherView];
  870. break;
  871. case ALAttributeBaseline: // same value as ALAttributeLastBaseline
  872. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeBaseline.");
  873. constraint = [self autoAlignAxis:ALAxisBaseline toSameAxisOfView:otherView];
  874. break;
  875. #if __PureLayout_MinBaseSDK_iOS_8_0
  876. case ALAttributeFirstBaseline:
  877. NSAssert(__PureLayout_MinSysVer_iOS_8_0, @"ALAttributeFirstBaseline is only supported on iOS 8.0 or higher.");
  878. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeFirstBaseline.");
  879. constraint = [self autoAlignAxis:ALAxisFirstBaseline toSameAxisOfView:otherView];
  880. break;
  881. #endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
  882. case ALAttributeTop:
  883. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeTop.");
  884. constraint = [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:otherView];
  885. break;
  886. case ALAttributeLeft:
  887. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeLeft.");
  888. constraint = [self autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:otherView];
  889. break;
  890. case ALAttributeBottom:
  891. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeBottom.");
  892. constraint = [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:otherView];
  893. break;
  894. case ALAttributeRight:
  895. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeRight.");
  896. constraint = [self autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:otherView];
  897. break;
  898. case ALAttributeLeading:
  899. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeLeading.");
  900. constraint = [self autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:otherView];
  901. break;
  902. case ALAttributeTrailing:
  903. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeTrailing.");
  904. constraint = [self autoPinEdge:ALEdgeTrailing toEdge:ALEdgeTrailing ofView:otherView];
  905. break;
  906. default:
  907. NSAssert(nil, @"Unsupported alignment option.");
  908. break;
  909. }
  910. return constraint;
  911. }
  912. @end