ALView+PureLayout.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // ALView+PureLayout.h
  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 "PureLayoutDefines.h"
  29. PL__ASSUME_NONNULL_BEGIN
  30. #pragma mark ALView+PureLayout
  31. /**
  32. A category on UIView/NSView that provides a simple yet powerful interface for creating Auto Layout constraints.
  33. */
  34. @interface ALView (PureLayout)
  35. #pragma mark Factory & Initializer Methods
  36. /** Creates and returns a new view that does not convert the autoresizing mask into constraints. */
  37. + (instancetype)newAutoLayoutView;
  38. /** Initializes and returns a new view that does not convert the autoresizing mask into constraints. */
  39. - (instancetype)initForAutoLayout;
  40. /** Configures an existing view to not convert the autoresizing mask into constraints and returns the view. */
  41. - (instancetype)configureForAutoLayout;
  42. #pragma mark Center & Align in Superview
  43. /** Centers the view in its superview. */
  44. - (PL__NSArray_of(NSLayoutConstraint *) *)autoCenterInSuperview;
  45. /** Aligns the view to the same axis of its superview. */
  46. - (NSLayoutConstraint *)autoAlignAxisToSuperviewAxis:(ALAxis)axis;
  47. #if PL__PureLayout_MinBaseSDK_iOS_8_0
  48. /** Centers the view in its superview's margins. Available in iOS 8.0 and later. */
  49. - (PL__NSArray_of(NSLayoutConstraint *) *)autoCenterInSuperviewMargins;
  50. /** Aligns the view to the corresponding margin axis of its superview. Available in iOS 8.0 and later. */
  51. - (NSLayoutConstraint *)autoAlignAxisToSuperviewMarginAxis:(ALAxis)axis;
  52. #endif /* PL__PureLayout_MinBaseSDK_iOS_8_0 */
  53. #pragma mark Pin Edges to Superview
  54. /** Pins the given edge of the view to the same edge of its superview. */
  55. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge;
  56. /** Pins the given edge of the view to the same edge of its superview with an inset. */
  57. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset;
  58. /** Pins the given edge of the view to the same edge of its superview with an inset as a maximum or minimum. */
  59. - (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset relation:(NSLayoutRelation)relation;
  60. /** Pins the edges of the view to the edges of its superview. */
  61. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdges;
  62. /** Pins the edges of the view to the edges of its superview with the given edge insets. */
  63. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets;
  64. /** Pins 3 of the 4 edges of the view to the edges of its superview with the given edge insets, excluding one edge. */
  65. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets excludingEdge:(ALEdge)edge;
  66. #if PL__PureLayout_MinBaseSDK_iOS_8_0
  67. /** Pins the given edge of the view to the corresponding margin of its superview. Available in iOS 8.0 and later. */
  68. - (NSLayoutConstraint *)autoPinEdgeToSuperviewMargin:(ALEdge)edge;
  69. /** Pins the given edge of the view to the corresponding margin of its superview as a maximum or minimum. Available in iOS 8.0 and later. */
  70. - (NSLayoutConstraint *)autoPinEdgeToSuperviewMargin:(ALEdge)edge relation:(NSLayoutRelation)relation;
  71. /** Pins the edges of the view to the margins of its superview. Available in iOS 8.0 and later. */
  72. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewMargins;
  73. /** Pins 3 of the 4 edges of the view to the margins of its superview excluding one edge. Available in iOS 8.0 and later. */
  74. - (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewMarginsExcludingEdge:(ALEdge)edge;
  75. #endif /* PL__PureLayout_MinBaseSDK_iOS_8_0 */
  76. #pragma mark Pin Edges
  77. /** Pins an edge of the view to a given edge of another view. */
  78. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView;
  79. /** Pins an edge of the view to a given edge of another view with an offset. */
  80. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView withOffset:(CGFloat)offset;
  81. /** Pins an edge of the view to a given edge of another view with an offset as a maximum or minimum. */
  82. - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation;
  83. #pragma mark Align Axes
  84. /** Aligns an axis of the view to the same axis of another view. */
  85. - (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView;
  86. /** Aligns an axis of the view to the same axis of another view with an offset. */
  87. - (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView withOffset:(CGFloat)offset;
  88. /** Aligns an axis of the view to the same axis of another view with a multiplier. */
  89. - (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView withMultiplier:(CGFloat)multiplier;
  90. #pragma mark Match Dimensions
  91. /** Matches a dimension of the view to a given dimension of another view. */
  92. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView;
  93. /** Matches a dimension of the view to a given dimension of another view with an offset. */
  94. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withOffset:(CGFloat)offset;
  95. /** Matches a dimension of the view to a given dimension of another view with an offset as a maximum or minimum. */
  96. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation;
  97. /** Matches a dimension of the view to a multiple of a given dimension of another view. */
  98. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier;
  99. /** Matches a dimension of the view to a multiple of a given dimension of another view as a maximum or minimum. */
  100. - (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation;
  101. #pragma mark Set Dimensions
  102. /** Sets the view to a specific size. */
  103. - (PL__NSArray_of(NSLayoutConstraint *) *)autoSetDimensionsToSize:(CGSize)size;
  104. /** Sets the given dimension of the view to a specific size. */
  105. - (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size;
  106. /** Sets the given dimension of the view to a specific size as a maximum or minimum. */
  107. - (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size relation:(NSLayoutRelation)relation;
  108. #pragma mark Set Content Compression Resistance & Hugging
  109. /** Sets the priority of content compression resistance for an axis.
  110. NOTE: This method must be called from within the block passed into the method +[NSLayoutConstraint autoSetPriority:forConstraints:] */
  111. - (void)autoSetContentCompressionResistancePriorityForAxis:(ALAxis)axis;
  112. /** Sets the priority of content hugging for an axis.
  113. NOTE: This method must be called from within the block passed into the method +[NSLayoutConstraint autoSetPriority:forConstraints:] */
  114. - (void)autoSetContentHuggingPriorityForAxis:(ALAxis)axis;
  115. #pragma mark Constrain Any Attributes
  116. /** Constrains an attribute of the view to a given attribute of another view. */
  117. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView;
  118. /** Constrains an attribute of the view to a given attribute of another view with an offset. */
  119. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withOffset:(CGFloat)offset;
  120. /** Constrains an attribute of the view to a given attribute of another view with an offset as a maximum or minimum. */
  121. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation;
  122. /** Constrains an attribute of the view to a given attribute of another view with a multiplier. */
  123. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier;
  124. /** Constrains an attribute of the view to a given attribute of another view with a multiplier as a maximum or minimum. */
  125. - (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation;
  126. #pragma mark Pin to Layout Guides (iOS only)
  127. #if TARGET_OS_IPHONE
  128. /** Pins the top edge of the view to the top layout guide of the given view controller with an inset. Available on iOS only. */
  129. - (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset;
  130. /** Pins the top edge of the view to the top layout guide of the given view controller with an inset as a maximum or minimum. Available on iOS only. */
  131. - (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset relation:(NSLayoutRelation)relation;
  132. /** Pins the bottom edge of the view to the bottom layout guide of the given view controller with an inset. Available on iOS only. */
  133. - (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset;
  134. /** Pins the bottom edge of the view to the bottom layout guide of the given view controller with an inset as a maximum or minimum. Available on iOS only. */
  135. - (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset relation:(NSLayoutRelation)relation;
  136. #endif /* TARGET_OS_IPHONE */
  137. @end
  138. PL__ASSUME_NONNULL_END