ALView+PureLayout.m 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. //
  2. // ALView+PureLayout.m
  3. // https://github.com/PureLayout/PureLayout
  4. //
  5. // Copyright (c) 2012 Richard Turton
  6. // Copyright (c) 2013-2015 Tyler Fox
  7. //
  8. // This code is distributed under the terms and conditions of the MIT license.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to
  12. // deal in the Software without restriction, including without limitation the
  13. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  14. // sell copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  26. // IN THE SOFTWARE.
  27. //
  28. #import "ALView+PureLayout.h"
  29. #import "NSLayoutConstraint+PureLayout.h"
  30. #import "NSArray+PureLayout.h"
  31. #import "PureLayout+Internal.h"
  32. #pragma mark - ALView+PureLayout
  33. @implementation ALView (PureLayout)
  34. #pragma mark Factory & Initializer Methods
  35. /**
  36. Creates and returns a new view that does not convert the autoresizing mask into constraints.
  37. */
  38. + (instancetype)newAutoLayoutView
  39. {
  40. ALView *view = [self new];
  41. view.translatesAutoresizingMaskIntoConstraints = NO;
  42. return view;
  43. }
  44. /**
  45. Initializes and returns a new view that does not convert the autoresizing mask into constraints.
  46. */
  47. - (instancetype)initForAutoLayout
  48. {
  49. self = [self init];
  50. if (self) {
  51. self.translatesAutoresizingMaskIntoConstraints = NO;
  52. }
  53. return self;
  54. }
  55. /**
  56. Configures an existing view to not convert the autoresizing mask into constraints and returns the view.
  57. */
  58. - (instancetype)configureForAutoLayout
  59. {
  60. self.translatesAutoresizingMaskIntoConstraints = NO;
  61. return self;
  62. }
  63. #pragma mark Center in Superview
  64. /**
  65. Centers the view in its superview.
  66. @return An array of constraints added.
  67. */
  68. - (PL__NSArray_of(NSLayoutConstraint *) *)autoCenterInSuperview
  69. {
  70. PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
  71. [constraints addObject:[self autoAlignAxisToSuperviewAxis:ALAxisHorizontal]];
  72. [constraints addObject:[self autoAlignAxisToSuperviewAxis:ALAxisVertical]];
  73. return constraints;
  74. }
  75. /**
  76. Aligns the view to the same axis of its superview.
  77. @param axis The axis of this view and of its superview to align.
  78. @return The constraint added.
  79. */
  80. - (NSLayoutConstraint *)autoAlignAxisToSuperviewAxis:(ALAxis)axis
  81. {
  82. self.translatesAutoresizingMaskIntoConstraints = NO;
  83. ALView *superview = self.superview;
  84. NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
  85. return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)axis ofView:superview];
  86. }
  87. #if PL__PureLayout_MinBaseSDK_iOS_8_0
  88. /**
  89. Centers the view in its superview, taking into account the layout margins of both the view and its superview.
  90. @return An array of constraints added.
  91. */
  92. - (PL__NSArray_of(NSLayoutConstraint *) *)autoCenterInSuperviewMargins
  93. {
  94. PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
  95. [constraints addObject:[self autoAlignAxisToSuperviewMarginAxis:ALAxisHorizontal]];
  96. [constraints addObject:[self autoAlignAxisToSuperviewMarginAxis:ALAxisVertical]];
  97. return constraints;
  98. }
  99. /**
  100. Aligns the view to the corresponding margin axis of its superview.
  101. @param axis The axis of this view to align to the corresponding margin axis of its superview.
  102. @return The constraint added.
  103. */
  104. - (NSLayoutConstraint *)autoAlignAxisToSuperviewMarginAxis:(ALAxis)axis
  105. {
  106. self.translatesAutoresizingMaskIntoConstraints = NO;
  107. ALView *superview = self.superview;
  108. NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
  109. ALMarginAxis marginAxis = [NSLayoutConstraint al_marginAxisForAxis:axis];
  110. return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)marginAxis ofView:superview];
  111. }
  112. #endif /* PL__PureLayout_MinBaseSDK_iOS_8_0 */
  113. #pragma mark Pin Edges to Superview
  114. /**
  115. Pins the given edge of the view to the same edge of its superview.
  116. @param edge The edge of this view and its superview to pin.
  117. @return The constraint added.
  118. */
  119. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge
  120. {
  121. return [self autoPinEdgeToSuperviewEdge:edge withInset:0.0];
  122. }
  123. /**
  124. Pins the given edge of the view to the same edge of its superview with an inset.
  125. @param edge The edge of this view and its superview to pin.
  126. @param inset The amount to inset this view's edge from the superview's edge.
  127. @return The constraint added.
  128. */
  129. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset
  130. {
  131. return [self autoPinEdgeToSuperviewEdge:edge withInset:inset relation:NSLayoutRelationEqual];
  132. }
  133. /**
  134. Pins the given edge of the view to the same edge of its superview with an inset as a maximum or minimum.
  135. @param edge The edge of this view and its superview to pin.
  136. @param inset The amount to inset this view's edge from the superview's edge.
  137. @param relation Whether the inset should be at least, at most, or exactly equal to the given value.
  138. @return The constraint added.
  139. */
  140. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset relation:(NSLayoutRelation)relation
  141. {
  142. self.translatesAutoresizingMaskIntoConstraints = NO;
  143. ALView *superview = self.superview;
  144. NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
  145. if (edge == ALEdgeBottom || edge == ALEdgeRight || edge == ALEdgeTrailing) {
  146. // The bottom, right, and trailing insets (and relations, if an inequality) are inverted to become offsets
  147. inset = -inset;
  148. if (relation == NSLayoutRelationLessThanOrEqual) {
  149. relation = NSLayoutRelationGreaterThanOrEqual;
  150. } else if (relation == NSLayoutRelationGreaterThanOrEqual) {
  151. relation = NSLayoutRelationLessThanOrEqual;
  152. }
  153. }
  154. return [self autoPinEdge:edge toEdge:edge ofView:superview withOffset:inset relation:relation];
  155. }
  156. /**
  157. Pins the edges of the view to the edges of its superview.
  158. @return An array of constraints added, ordered counterclockwise from top.
  159. */
  160. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdges
  161. {
  162. return [self autoPinEdgesToSuperviewEdgesWithInsets:ALEdgeInsetsZero];
  163. }
  164. /**
  165. Pins the edges of the view to the edges of its superview with the given edge insets.
  166. The insets.left corresponds to a leading edge constraint, and insets.right corresponds to a trailing edge constraint.
  167. @param insets The insets for this view's edges from its superview's edges.
  168. @return An array of constraints added, ordered counterclockwise from top.
  169. */
  170. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets
  171. {
  172. PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
  173. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:insets.top]];
  174. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:insets.left]];
  175. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:insets.bottom]];
  176. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:insets.right]];
  177. return constraints;
  178. }
  179. /**
  180. Pins 3 of the 4 edges of the view to the edges of its superview with the given edge insets, excluding one edge.
  181. The insets.left corresponds to a leading edge constraint, and insets.right corresponds to a trailing edge constraint.
  182. @param insets The insets for this view's edges from its superview's edges. The inset corresponding to the excluded edge
  183. will be ignored.
  184. @param edge The edge of this view to exclude in pinning to its superview; this method will not apply any constraint to it.
  185. @return An array of constraints added, ordered counterclockwise from top.
  186. */
  187. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets excludingEdge:(ALEdge)edge
  188. {
  189. PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
  190. if (edge != ALEdgeTop) {
  191. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:insets.top]];
  192. }
  193. if (edge != ALEdgeLeading && edge != ALEdgeLeft) {
  194. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:insets.left]];
  195. }
  196. if (edge != ALEdgeBottom) {
  197. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:insets.bottom]];
  198. }
  199. if (edge != ALEdgeTrailing && edge != ALEdgeRight) {
  200. [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:insets.right]];
  201. }
  202. return constraints;
  203. }
  204. #if PL__PureLayout_MinBaseSDK_iOS_8_0
  205. /**
  206. Pins the given edge of the view to the corresponding margin of its superview.
  207. @param edge The edge of this view to pin to the corresponding margin of its superview.
  208. @return The constraint added.
  209. */
  210. - (NSLayoutConstraint *)autoPinEdgeToSuperviewMargin:(ALEdge)edge
  211. {
  212. return [self autoPinEdgeToSuperviewMargin:edge relation:NSLayoutRelationEqual];
  213. }
  214. /**
  215. Pins the given edge of the view to the corresponding margin of its superview as a maximum or minimum.
  216. @param edge The edge of this view to pin to the corresponding margin of its superview.
  217. @param relation Whether the edge should be inset by at least, at most, or exactly the superview's margin.
  218. @return The constraint added.
  219. */
  220. - (NSLayoutConstraint *)autoPinEdgeToSuperviewMargin:(ALEdge)edge relation:(NSLayoutRelation)relation
  221. {
  222. self.translatesAutoresizingMaskIntoConstraints = NO;
  223. ALView *superview = self.superview;
  224. NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
  225. if (edge == ALEdgeBottom || edge == ALEdgeRight || edge == ALEdgeTrailing) {
  226. // The bottom, right, and trailing relations are inverted
  227. if (relation == NSLayoutRelationLessThanOrEqual) {
  228. relation = NSLayoutRelationGreaterThanOrEqual;
  229. } else if (relation == NSLayoutRelationGreaterThanOrEqual) {
  230. relation = NSLayoutRelationLessThanOrEqual;
  231. }
  232. }
  233. ALMargin margin = [NSLayoutConstraint al_marginForEdge:edge];
  234. return [self autoConstrainAttribute:(ALAttribute)edge toAttribute:(ALAttribute)margin ofView:superview withOffset:0.0 relation:relation];
  235. }
  236. /**
  237. Pins the edges of the view to the margins of its superview.
  238. @return An array of constraints added, ordered counterclockwise from top.
  239. */
  240. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewMargins
  241. {
  242. PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
  243. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeTop]];
  244. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeLeading]];
  245. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeBottom]];
  246. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeTrailing]];
  247. return constraints;
  248. }
  249. /**
  250. Pins 3 of the 4 edges of the view to the margins of its superview, excluding one edge.
  251. @param edge The edge of this view to exclude in pinning to its superview; this method will not apply any constraint to it.
  252. @return An array of constraints added, ordered counterclockwise from top.
  253. */
  254. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewMarginsExcludingEdge:(ALEdge)edge
  255. {
  256. PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
  257. if (edge != ALEdgeTop) {
  258. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeTop]];
  259. }
  260. if (edge != ALEdgeLeading && edge != ALEdgeLeft) {
  261. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeLeading]];
  262. }
  263. if (edge != ALEdgeBottom) {
  264. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeBottom]];
  265. }
  266. if (edge != ALEdgeTrailing && edge != ALEdgeRight) {
  267. [constraints addObject:[self autoPinEdgeToSuperviewMargin:ALEdgeTrailing]];
  268. }
  269. return constraints;
  270. }
  271. #endif /* PL__PureLayout_MinBaseSDK_iOS_8_0 */
  272. #pragma mark Pin Edges
  273. /**
  274. Pins an edge of the view to a given edge of another view.
  275. @param edge The edge of this view to pin.
  276. @param toEdge The edge of the other view to pin to.
  277. @param otherView The other view to pin to. Must be in the same view hierarchy as this view.
  278. @return The constraint added.
  279. */
  280. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView
  281. {
  282. return [self autoPinEdge:edge toEdge:toEdge ofView:otherView withOffset:0.0];
  283. }
  284. /**
  285. Pins an edge of the view to a given edge of another view with an offset.
  286. @param edge The edge of this view to pin.
  287. @param toEdge The edge of the other view to pin to.
  288. @param otherView The other view to pin to. Must be in the same view hierarchy as this view.
  289. @param offset The offset between the edge of this view and the edge of the other view.
  290. @return The constraint added.
  291. */
  292. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView withOffset:(CGFloat)offset
  293. {
  294. return [self autoPinEdge:edge toEdge:toEdge ofView:otherView withOffset:offset relation:NSLayoutRelationEqual];
  295. }
  296. /**
  297. Pins an edge of the view to a given edge of another view with an offset as a maximum or minimum.
  298. @param edge The edge of this view to pin.
  299. @param toEdge The edge of the other view to pin to.
  300. @param otherView The other view to pin to. Must be in the same view hierarchy as this view.
  301. @param offset The offset between the edge of this view and the edge of the other view.
  302. @param relation Whether the offset should be at least, at most, or exactly equal to the given value.
  303. @return The constraint added.
  304. */
  305. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation
  306. {
  307. return [self autoConstrainAttribute:(ALAttribute)edge toAttribute:(ALAttribute)toEdge ofView:otherView withOffset:offset relation:relation];
  308. }
  309. #pragma mark Align Axes
  310. /**
  311. Aligns an axis of the view to the same axis of another view.
  312. @param axis The axis of this view and the other view to align.
  313. @param otherView The other view to align to. Must be in the same view hierarchy as this view.
  314. @return The constraint added.
  315. */
  316. - (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView
  317. {
  318. return [self autoAlignAxis:axis toSameAxisOfView:otherView withOffset:0.0];
  319. }
  320. /**
  321. Aligns an axis of the view to the same axis of another view with an offset.
  322. @param axis The axis of this view and the other view to align.
  323. @param otherView The other view to align to. Must be in the same view hierarchy as this view.
  324. @param offset The offset between the axis of this view and the axis of the other view.
  325. @return The constraint added.
  326. */
  327. - (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView withOffset:(CGFloat)offset
  328. {
  329. return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)axis ofView:otherView withOffset:offset];
  330. }
  331. /**
  332. Aligns an axis of the view to the same axis of another view with a multiplier.
  333. @param axis The axis of this view and the other view to align.
  334. @param otherView The other view to align to. Must be in the same view hierarchy as this view.
  335. @param multiplier The multiplier between the axis of this view and the axis of the other view.
  336. @return The constraint added.
  337. */
  338. - (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView withMultiplier:(CGFloat)multiplier
  339. {
  340. return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)axis ofView:otherView withMultiplier:multiplier];
  341. }
  342. #pragma mark Match Dimensions
  343. /**
  344. Matches a dimension of the view to a given dimension of another view.
  345. @param dimension The dimension of this view to pin.
  346. @param toDimension The dimension of the other view to pin to.
  347. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  348. @return The constraint added.
  349. */
  350. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView
  351. {
  352. return [self autoMatchDimension:dimension toDimension:toDimension ofView:otherView withOffset:0.0];
  353. }
  354. /**
  355. Matches a dimension of the view to a given dimension of another view with an offset.
  356. @param dimension The dimension of this view to pin.
  357. @param toDimension The dimension of the other view to pin to.
  358. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  359. @param offset The offset between the dimension of this view and the dimension of the other view.
  360. @return The constraint added.
  361. */
  362. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withOffset:(CGFloat)offset
  363. {
  364. return [self autoMatchDimension:dimension toDimension:toDimension ofView:otherView withOffset:offset relation:NSLayoutRelationEqual];
  365. }
  366. /**
  367. Matches a dimension of the view to a given dimension of another view with an offset as a maximum or minimum.
  368. @param dimension The dimension of this view to pin.
  369. @param toDimension The dimension of the other view to pin to.
  370. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  371. @param offset The offset between the dimension of this view and the dimension of the other view.
  372. @param relation Whether the offset should be at least, at most, or exactly equal to the given value.
  373. @return The constraint added.
  374. */
  375. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation
  376. {
  377. return [self autoConstrainAttribute:(ALAttribute)dimension toAttribute:(ALAttribute)toDimension ofView:otherView withOffset:offset relation:relation];
  378. }
  379. /**
  380. Matches a dimension of the view to a multiple of a given dimension of another view.
  381. @param dimension The dimension of this view to pin.
  382. @param toDimension The dimension of the other view to pin to.
  383. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  384. @param multiplier The multiple of the other view's given dimension that this view's given dimension should be.
  385. @return The constraint added.
  386. */
  387. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier
  388. {
  389. return [self autoMatchDimension:dimension toDimension:toDimension ofView:otherView withMultiplier:multiplier relation:NSLayoutRelationEqual];
  390. }
  391. /**
  392. Matches a dimension of the view to a multiple of a given dimension of another view as a maximum or minimum.
  393. @param dimension The dimension of this view to pin.
  394. @param toDimension The dimension of the other view to pin to.
  395. @param otherView The other view to match to. Must be in the same view hierarchy as this view.
  396. @param multiplier The multiple of the other view's given dimension that this view's given dimension should be.
  397. @param relation Whether the multiple should be at least, at most, or exactly equal to the given value.
  398. @return The constraint added.
  399. */
  400. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation
  401. {
  402. return [self autoConstrainAttribute:(ALAttribute)dimension toAttribute:(ALAttribute)toDimension ofView:otherView withMultiplier:multiplier relation:relation];
  403. }
  404. #pragma mark Set Dimensions
  405. /**
  406. Sets the view to a specific size.
  407. @param size The size to set this view's dimensions to.
  408. @return An array of constraints added.
  409. */
  410. - (PL__NSArray_of(NSLayoutConstraint *) *)autoSetDimensionsToSize:(CGSize)size
  411. {
  412. PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
  413. [constraints addObject:[self autoSetDimension:ALDimensionWidth toSize:size.width]];
  414. [constraints addObject:[self autoSetDimension:ALDimensionHeight toSize:size.height]];
  415. return constraints;
  416. }
  417. /**
  418. Sets the given dimension of the view to a specific size.
  419. @param dimension The dimension of this view to set.
  420. @param size The size to set the given dimension to.
  421. @return The constraint added.
  422. */
  423. - (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size
  424. {
  425. return [self autoSetDimension:dimension toSize:size relation:NSLayoutRelationEqual];
  426. }
  427. /**
  428. Sets the given dimension of the view to a specific size as a maximum or minimum.
  429. @param dimension The dimension of this view to set.
  430. @param size The size to set the given dimension to.
  431. @param relation Whether the size should be at least, at most, or exactly equal to the given value.
  432. @return The constraint added.
  433. */
  434. - (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size relation:(NSLayoutRelation)relation
  435. {
  436. self.translatesAutoresizingMaskIntoConstraints = NO;
  437. NSLayoutAttribute layoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:(ALAttribute)dimension];
  438. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:layoutAttribute relatedBy:relation toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:size];
  439. [constraint autoInstall];
  440. return constraint;
  441. }
  442. #pragma mark Set Content Compression Resistance & Hugging
  443. /**
  444. Sets the priority of content compression resistance for an axis.
  445. NOTE: This method must be called from within the block passed into the method +[autoSetPriority:forConstraints:]
  446. @param axis The axis to set the content compression resistance priority for.
  447. */
  448. - (void)autoSetContentCompressionResistancePriorityForAxis:(ALAxis)axis
  449. {
  450. NSAssert([NSLayoutConstraint al_isExecutingPriorityConstraintsBlock], @"%@ should only be called from within the block passed into the method +[autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
  451. if ([NSLayoutConstraint al_isExecutingPriorityConstraintsBlock]) {
  452. self.translatesAutoresizingMaskIntoConstraints = NO;
  453. ALLayoutConstraintAxis constraintAxis = [NSLayoutConstraint al_constraintAxisForAxis:axis];
  454. #if TARGET_OS_IPHONE
  455. [self setContentCompressionResistancePriority:[NSLayoutConstraint al_currentGlobalConstraintPriority] forAxis:constraintAxis];
  456. #else
  457. [self setContentCompressionResistancePriority:[NSLayoutConstraint al_currentGlobalConstraintPriority] forOrientation:constraintAxis];
  458. #endif /* TARGET_OS_IPHONE */
  459. }
  460. }
  461. /**
  462. Sets the priority of content hugging for an axis.
  463. NOTE: This method must be called from within the block passed into the method +[autoSetPriority:forConstraints:]
  464. @param axis The axis to set the content hugging priority for.
  465. */
  466. - (void)autoSetContentHuggingPriorityForAxis:(ALAxis)axis
  467. {
  468. NSAssert([NSLayoutConstraint al_isExecutingPriorityConstraintsBlock], @"%@ should only be called from within the block passed into the method +[autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
  469. if ([NSLayoutConstraint al_isExecutingPriorityConstraintsBlock]) {
  470. self.translatesAutoresizingMaskIntoConstraints = NO;
  471. ALLayoutConstraintAxis constraintAxis = [NSLayoutConstraint al_constraintAxisForAxis:axis];
  472. #if TARGET_OS_IPHONE
  473. [self setContentHuggingPriority:[NSLayoutConstraint al_currentGlobalConstraintPriority] forAxis:constraintAxis];
  474. #else
  475. [self setContentHuggingPriority:[NSLayoutConstraint al_currentGlobalConstraintPriority] forOrientation:constraintAxis];
  476. #endif /* TARGET_OS_IPHONE */
  477. }
  478. }
  479. #pragma mark Constrain Any Attributes
  480. /**
  481. Constrains an attribute of the view to a given attribute of another view.
  482. This method can be used to constrain different types of attributes across two views.
  483. @param attribute Any attribute of this view to constrain.
  484. @param toAttribute Any attribute of the other view to constrain to.
  485. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  486. @return The constraint added.
  487. */
  488. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView
  489. {
  490. return [self autoConstrainAttribute:attribute toAttribute:toAttribute ofView:otherView withOffset:0.0];
  491. }
  492. /**
  493. Constrains an attribute of the view to a given attribute of another view with an offset.
  494. This method can be used to constrain different types of attributes across two views.
  495. @param attribute Any attribute of this view to constrain.
  496. @param toAttribute Any attribute of the other view to constrain to.
  497. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  498. @param offset The offset between the attribute of this view and the attribute of the other view.
  499. @return The constraint added.
  500. */
  501. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withOffset:(CGFloat)offset
  502. {
  503. return [self autoConstrainAttribute:attribute toAttribute:toAttribute ofView:otherView withOffset:offset relation:NSLayoutRelationEqual];
  504. }
  505. /**
  506. Constrains an attribute of the view to a given attribute of another view with an offset as a maximum or minimum.
  507. This method can be used to constrain different types of attributes across two views.
  508. @param attribute Any attribute of this view to constrain.
  509. @param toAttribute Any attribute of the other view to constrain to.
  510. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  511. @param offset The offset between the attribute of this view and the attribute of the other view.
  512. @param relation Whether the offset should be at least, at most, or exactly equal to the given value.
  513. @return The constraint added.
  514. */
  515. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation
  516. {
  517. self.translatesAutoresizingMaskIntoConstraints = NO;
  518. NSLayoutAttribute layoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:attribute];
  519. NSLayoutAttribute toLayoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:toAttribute];
  520. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:layoutAttribute relatedBy:relation toItem:otherView attribute:toLayoutAttribute multiplier:1.0 constant:offset];
  521. [constraint autoInstall];
  522. return constraint;
  523. }
  524. /**
  525. Constrains an attribute of the view to a given attribute of another view with a multiplier.
  526. This method can be used to constrain different types of attributes across two views.
  527. @param attribute Any attribute of this view to constrain.
  528. @param toAttribute Any attribute of the other view to constrain to.
  529. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  530. @param multiplier The multiplier between the attribute of this view and the attribute of the other view.
  531. @return The constraint added.
  532. */
  533. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier
  534. {
  535. return [self autoConstrainAttribute:attribute toAttribute:toAttribute ofView:otherView withMultiplier:multiplier relation:NSLayoutRelationEqual];
  536. }
  537. /**
  538. Constrains an attribute of the view to a given attribute of another view with a multiplier as a maximum or minimum.
  539. This method can be used to constrain different types of attributes across two views.
  540. @param attribute Any attribute of this view to constrain.
  541. @param toAttribute Any attribute of the other view to constrain to.
  542. @param otherView The other view to constrain to. Must be in the same view hierarchy as this view.
  543. @param multiplier The multiplier between the attribute of this view and the attribute of the other view.
  544. @param relation Whether the multiplier should be at least, at most, or exactly equal to the given value.
  545. @return The constraint added.
  546. */
  547. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation
  548. {
  549. self.translatesAutoresizingMaskIntoConstraints = NO;
  550. NSLayoutAttribute layoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:attribute];
  551. NSLayoutAttribute toLayoutAttribute = [NSLayoutConstraint al_layoutAttributeForAttribute:toAttribute];
  552. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:layoutAttribute relatedBy:relation toItem:otherView attribute:toLayoutAttribute multiplier:multiplier constant:0.0];
  553. [constraint autoInstall];
  554. return constraint;
  555. }
  556. #pragma mark Pin to Layout Guides
  557. #if TARGET_OS_IPHONE
  558. /**
  559. Pins the top edge of the view to the top layout guide of the given view controller with an inset.
  560. For compatibility with iOS 6 (where layout guides do not exist), this method will simply pin the top edge of
  561. the view to the top edge of the given view controller's view with an inset.
  562. @param viewController The view controller whose topLayoutGuide should be used to pin to.
  563. @param inset The amount to inset this view's top edge from the layout guide.
  564. @return The constraint added.
  565. */
  566. - (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset
  567. {
  568. return [self autoPinToTopLayoutGuideOfViewController:viewController withInset:inset relation:NSLayoutRelationEqual];
  569. }
  570. - (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset relation:(NSLayoutRelation)relation
  571. {
  572. if (PL__PureLayout_MinSysVer_iOS_7_0) {
  573. self.translatesAutoresizingMaskIntoConstraints = NO;
  574. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:relation toItem:viewController.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:inset];
  575. [viewController.view al_addConstraint:constraint]; // Can't use autoInstall because the layout guide is not a view
  576. return constraint;
  577. } else {
  578. // iOS 6 fallback
  579. return [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:viewController.view withOffset:inset relation:relation];
  580. }
  581. }
  582. /**
  583. Pins the bottom edge of the view to the bottom layout guide of the given view controller with an inset.
  584. For compatibility with iOS 6 (where layout guides do not exist), this method will simply pin the bottom edge of
  585. the view to the bottom edge of the given view controller's view with an inset.
  586. @param viewController The view controller whose bottomLayoutGuide should be used to pin to.
  587. @param inset The amount to inset this view's bottom edge from the layout guide.
  588. @return The constraint added.
  589. */
  590. - (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset
  591. {
  592. return [self autoPinToBottomLayoutGuideOfViewController:viewController withInset:inset relation:NSLayoutRelationEqual];
  593. }
  594. - (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset relation:(NSLayoutRelation)relation
  595. {
  596. // The bottom inset (and relation, if an inequality) is inverted to become an offset
  597. inset = -inset;
  598. if (relation == NSLayoutRelationLessThanOrEqual) {
  599. relation = NSLayoutRelationGreaterThanOrEqual;
  600. } else if (relation == NSLayoutRelationGreaterThanOrEqual) {
  601. relation = NSLayoutRelationLessThanOrEqual;
  602. }
  603. if (PL__PureLayout_MinSysVer_iOS_7_0) {
  604. self.translatesAutoresizingMaskIntoConstraints = NO;
  605. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:relation toItem:viewController.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:inset];
  606. [viewController.view al_addConstraint:constraint]; // Can't use autoInstall because the layout guide is not a view
  607. return constraint;
  608. } else {
  609. // iOS 6 fallback
  610. return [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:viewController.view withOffset:inset relation:relation];
  611. }
  612. }
  613. #endif /* TARGET_OS_IPHONE */
  614. #pragma mark Internal Methods
  615. /**
  616. Adds the given constraint to this view after applying the global state to the constraint.
  617. NOTE: This method is compatible with all versions of iOS, and should be used for older versions before the active
  618. property on NSLayoutConstraint was introduced.
  619. This method should be the only one that calls the UIView/NSView addConstraint: method directly.
  620. @param constraint The constraint to set the global priority on and then add to this view.
  621. */
  622. - (void)al_addConstraint:(NSLayoutConstraint *)constraint
  623. {
  624. [NSLayoutConstraint al_applyGlobalStateToConstraint:constraint];
  625. if ([NSLayoutConstraint al_preventAutomaticConstraintInstallation]) {
  626. [[NSLayoutConstraint al_currentArrayOfCreatedConstraints] addObject:constraint];
  627. } else {
  628. [self addConstraint:constraint];
  629. }
  630. }
  631. /**
  632. Returns the common superview for this view and the given other view.
  633. Raises an exception if this view and the other view do not share a common superview.
  634. @return The common superview for the two views.
  635. */
  636. - (ALView *)al_commonSuperviewWithView:(ALView *)otherView
  637. {
  638. ALView *commonSuperview = nil;
  639. ALView *startView = self;
  640. do {
  641. #if TARGET_OS_IPHONE
  642. if ([otherView isDescendantOfView:startView]) {
  643. commonSuperview = startView;
  644. }
  645. #else
  646. if ([otherView isDescendantOf:startView]) {
  647. commonSuperview = startView;
  648. }
  649. #endif /* TARGET_OS_IPHONE */
  650. startView = startView.superview;
  651. } while (startView && !commonSuperview);
  652. 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.");
  653. return commonSuperview;
  654. }
  655. /**
  656. Aligns this view to another view with an alignment attribute.
  657. @param attribute The attribute to use to align the two views.
  658. @param otherView The other view to align to.
  659. @param axis The axis along which the views are distributed, used to validate the alignment attribute.
  660. @return The constraint added.
  661. */
  662. - (NSLayoutConstraint *)al_alignAttribute:(ALAttribute)attribute toView:(ALView *)otherView forAxis:(ALAxis)axis
  663. {
  664. NSLayoutConstraint *constraint = nil;
  665. switch (attribute) {
  666. case ALAttributeVertical:
  667. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeVertical.");
  668. constraint = [self autoAlignAxis:ALAxisVertical toSameAxisOfView:otherView];
  669. break;
  670. case ALAttributeHorizontal:
  671. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeHorizontal.");
  672. constraint = [self autoAlignAxis:ALAxisHorizontal toSameAxisOfView:otherView];
  673. break;
  674. case ALAttributeBaseline: // same value as ALAttributeLastBaseline
  675. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeBaseline.");
  676. constraint = [self autoAlignAxis:ALAxisBaseline toSameAxisOfView:otherView];
  677. break;
  678. #if PL__PureLayout_MinBaseSDK_iOS_8_0
  679. case ALAttributeFirstBaseline:
  680. NSAssert(PL__PureLayout_MinSysVer_iOS_8_0, @"ALAttributeFirstBaseline is only supported on iOS 8.0 or higher.");
  681. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeFirstBaseline.");
  682. constraint = [self autoAlignAxis:ALAxisFirstBaseline toSameAxisOfView:otherView];
  683. break;
  684. #endif /* PL__PureLayout_MinBaseSDK_iOS_8_0 */
  685. case ALAttributeTop:
  686. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeTop.");
  687. constraint = [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:otherView];
  688. break;
  689. case ALAttributeLeft:
  690. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeLeft.");
  691. constraint = [self autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:otherView];
  692. break;
  693. case ALAttributeBottom:
  694. NSAssert(axis != ALAxisVertical, @"Cannot align views that are distributed vertically with ALAttributeBottom.");
  695. constraint = [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:otherView];
  696. break;
  697. case ALAttributeRight:
  698. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeRight.");
  699. constraint = [self autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:otherView];
  700. break;
  701. case ALAttributeLeading:
  702. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeLeading.");
  703. constraint = [self autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:otherView];
  704. break;
  705. case ALAttributeTrailing:
  706. NSAssert(axis == ALAxisVertical, @"Cannot align views that are distributed horizontally with ALAttributeTrailing.");
  707. constraint = [self autoPinEdge:ALEdgeTrailing toEdge:ALEdgeTrailing ofView:otherView];
  708. break;
  709. // All of the below attributes are invalid as alignment options. Listing them explicitly (even though they just fall through to the default case) to avoid an incomplete switch statement warning from the compiler.
  710. case ALAttributeWidth:
  711. case ALAttributeHeight:
  712. #if PL__PureLayout_MinBaseSDK_iOS_8_0
  713. case ALAttributeMarginLeft:
  714. case ALAttributeMarginRight:
  715. case ALAttributeMarginTop:
  716. case ALAttributeMarginBottom:
  717. case ALAttributeMarginLeading:
  718. case ALAttributeMarginTrailing:
  719. case ALAttributeMarginAxisVertical:
  720. case ALAttributeMarginAxisHorizontal:
  721. #endif /* PL__PureLayout_MinBaseSDK_iOS_8_0 */
  722. default:
  723. NSAssert(nil, @"Unsupported attribute for alignment.");
  724. break;
  725. }
  726. return constraint;
  727. }
  728. @end