|
| 1 | +// |
| 2 | +// UIView+AutoLayout.h |
| 3 | +// v1.0.0 |
| 4 | +// https://github.com/smileyborg/UIView-AutoLayout |
| 5 | +// |
| 6 | +// Copyright (c) 2012 Richard Turton |
| 7 | +// Copyright (c) 2013 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 | + |
| 30 | +#import <UIKit/UIKit.h> |
| 31 | + |
| 32 | +typedef NS_ENUM(NSInteger, ALEdge) { |
| 33 | + ALEdgeTop = 0, // the top edge of the view |
| 34 | + ALEdgeLeft, // the left edge of the view |
| 35 | + ALEdgeBottom, // the bottom edge of the view |
| 36 | + ALEdgeRight, // the right edge of the view |
| 37 | + ALEdgeLeading, // the leading edge of the view (left edge for left-to-right languages like English, right edge for right-to-left languages like Arabic) |
| 38 | + ALEdgeTrailing // the trailing edge of the view (right edge for left-to-right languages like English, left edge for right-to-left languages like Arabic) |
| 39 | +}; |
| 40 | + |
| 41 | +typedef NS_ENUM(NSInteger, ALAxis) { |
| 42 | + ALAxisHorizontal = 0, // a horizontal line through the center of the view |
| 43 | + ALAxisVertical, // a vertical line through the center of the view |
| 44 | + ALAxisBaseline // a horizontal line at the text baseline (not applicable to all views) |
| 45 | +}; |
| 46 | + |
| 47 | +typedef NS_ENUM(NSInteger, ALDimension) { |
| 48 | + ALDimensionWidth = 0, // the width of the view |
| 49 | + ALDimensionHeight // the height of the view |
| 50 | +}; |
| 51 | + |
| 52 | +typedef void(^ALConstraintsBlock)(void); // a block of method calls to the UIView+AutoLayout category API |
| 53 | + |
| 54 | + |
| 55 | +#pragma mark - UIView+AutoLayout |
| 56 | + |
| 57 | +/** |
| 58 | + A category on UIView that provides a simple yet powerful interface for creating Auto Layout constraints. |
| 59 | + */ |
| 60 | +@interface UIView (AutoLayout) |
| 61 | + |
| 62 | + |
| 63 | +#pragma mark Factory & Initializer Methods |
| 64 | + |
| 65 | +/** Creates and returns a new view that does not convert the autoresizing mask into constraints. */ |
| 66 | ++ (instancetype)newAutoLayoutView; |
| 67 | + |
| 68 | +/** Initializes and returns a new view that does not convert the autoresizing mask into constraints. */ |
| 69 | +- (instancetype)initForAutoLayout; |
| 70 | + |
| 71 | + |
| 72 | +#pragma mark Auto Layout Convenience Methods |
| 73 | + |
| 74 | +/** Sets the constraint priority to the given value for all constraints created using the UIView+AutoLayout category API within the given constraints block. |
| 75 | + NOTE: This method will have no effect (and will NOT set the priority) on constraints created or added using the SDK directly within the block! */ |
| 76 | ++ (void)autoSetPriority:(UILayoutPriority)priority forConstraints:(ALConstraintsBlock)block; |
| 77 | + |
| 78 | + |
| 79 | +/** Removes the given constraint from the view it has been added to. */ |
| 80 | ++ (void)autoRemoveConstraint:(NSLayoutConstraint *)constraint; |
| 81 | + |
| 82 | +/** Removes the given constraints from the views they have been added to. */ |
| 83 | ++ (void)autoRemoveConstraints:(NSArray *)constraints; |
| 84 | + |
| 85 | +/** Removes all explicit constraints that affect the view. |
| 86 | + WARNING: Apple's constraint solver is not optimized for large-scale constraint changes; you may encounter major performance issues after using this method. |
| 87 | + NOTE: This method preserves implicit constraints, such as intrinsic content size constraints, which you usually do not want to remove. */ |
| 88 | +- (void)autoRemoveConstraintsAffectingView; |
| 89 | + |
| 90 | +/** Removes all constraints that affect the view, optionally including implicit constraints. |
| 91 | + WARNING: Apple's constraint solver is not optimized for large-scale constraint changes; you may encounter major performance issues after using this method. |
| 92 | + NOTE: Implicit constraints are auto-generated lower priority constraints, and you usually do not want to remove these. */ |
| 93 | +- (void)autoRemoveConstraintsAffectingViewIncludingImplicitConstraints:(BOOL)shouldRemoveImplicitConstraints; |
| 94 | + |
| 95 | +/** Recursively removes all explicit constraints that affect the view and its subviews. |
| 96 | + WARNING: Apple's constraint solver is not optimized for large-scale constraint changes; you may encounter major performance issues after using this method. |
| 97 | + NOTE: This method preserves implicit constraints, such as intrinsic content size constraints, which you usually do not want to remove. */ |
| 98 | +- (void)autoRemoveConstraintsAffectingViewAndSubviews; |
| 99 | + |
| 100 | +/** Recursively removes all constraints from the view and its subviews, optionally including implicit constraints. |
| 101 | + WARNING: Apple's constraint solver is not optimized for large-scale constraint changes; you may encounter major performance issues after using this method. |
| 102 | + NOTE: Implicit constraints are auto-generated lower priority constraints, and you usually do not want to remove these. */ |
| 103 | +- (void)autoRemoveConstraintsAffectingViewAndSubviewsIncludingImplicitConstraints:(BOOL)shouldRemoveImplicitConstraints; |
| 104 | + |
| 105 | + |
| 106 | +/** Centers the view in its superview. */ |
| 107 | +- (NSArray *)autoCenterInSuperview; |
| 108 | + |
| 109 | +/** Centers the view along the given axis (horizontal or vertical) within its superview. */ |
| 110 | +- (NSLayoutConstraint *)autoCenterInSuperviewAlongAxis:(ALAxis)axis; |
| 111 | + |
| 112 | + |
| 113 | +/** Pins the given center axis of the view to a fixed position (X or Y value, depending on axis) in the superview. */ |
| 114 | +- (NSLayoutConstraint *)autoPinCenterAxis:(ALAxis)axis toPositionInSuperview:(CGFloat)value; |
| 115 | + |
| 116 | +/** Pins the given edge of the view to a fixed position (X or Y value, depending on edge) in the superview. */ |
| 117 | +- (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toPositionInSuperview:(CGFloat)value; |
| 118 | + |
| 119 | + |
| 120 | +/** Pins the given edge of the view to the same edge of the superview with an inset. */ |
| 121 | +- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset; |
| 122 | + |
| 123 | +/** Pins the given edge of the view to the same edge of the superview with an inset as a maximum or minimum. */ |
| 124 | +- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset relation:(NSLayoutRelation)relation; |
| 125 | + |
| 126 | +/** Pins the edges of the view to the edges of its superview with the given edge insets. */ |
| 127 | +- (NSArray *)autoPinEdgesToSuperviewEdgesWithInsets:(UIEdgeInsets)insets; |
| 128 | + |
| 129 | + |
| 130 | +/** Pins an edge of the view to a given edge of another view. */ |
| 131 | +- (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(UIView *)peerView; |
| 132 | + |
| 133 | +/** Pins an edge of the view to a given edge of another view with an offset. */ |
| 134 | +- (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(UIView *)peerView withOffset:(CGFloat)offset; |
| 135 | + |
| 136 | +/** Pins an edge of the view to a given edge of another view with an offset as a maximum or minimum. */ |
| 137 | +- (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(UIView *)peerView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation; |
| 138 | + |
| 139 | + |
| 140 | +/** Aligns an axis of the view to the same axis of another view. */ |
| 141 | +- (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(UIView *)peerView; |
| 142 | + |
| 143 | +/** Aligns an axis of the view to the same axis of another view with an offset. */ |
| 144 | +- (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(UIView *)peerView withOffset:(CGFloat)offset; |
| 145 | + |
| 146 | + |
| 147 | +/** Matches a dimension of the view to a given dimension of another view. */ |
| 148 | +- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(UIView *)peerView; |
| 149 | + |
| 150 | +/** Matches a dimension of the view to a given dimension of another view with an offset. */ |
| 151 | +- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(UIView *)peerView withOffset:(CGFloat)offset; |
| 152 | + |
| 153 | +/** Matches a dimension of the view to a given dimension of another view with an offset as a maximum or minimum. */ |
| 154 | +- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(UIView *)peerView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation; |
| 155 | + |
| 156 | +/** Matches a dimension of the view to a multiple of a given dimension of another view. */ |
| 157 | +- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(UIView *)peerView withMultiplier:(CGFloat)multiplier; |
| 158 | + |
| 159 | +/** Matches a dimension of the view to a multiple of a given dimension of another view as a maximum or minimum. */ |
| 160 | +- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(UIView *)peerView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation; |
| 161 | + |
| 162 | + |
| 163 | +/** Sets the view to a specific size. */ |
| 164 | +- (NSArray *)autoSetDimensionsToSize:(CGSize)size; |
| 165 | + |
| 166 | +/** Sets the given dimension of the view to a specific size. */ |
| 167 | +- (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size; |
| 168 | + |
| 169 | +/** Sets the given dimension of the view to a specific size as a maximum or minimum. */ |
| 170 | +- (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size relation:(NSLayoutRelation)relation; |
| 171 | + |
| 172 | + |
| 173 | +/** Pins the top edge of the view to the top layout guide of the given view controller with an inset. */ |
| 174 | +- (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset; |
| 175 | + |
| 176 | +/** Pins the bottom edge of the view to the bottom layout guide of the given view controller with an inset. */ |
| 177 | +- (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset; |
| 178 | + |
| 179 | +@end |
| 180 | + |
| 181 | + |
| 182 | +#pragma mark - NSArray+AutoLayout |
| 183 | + |
| 184 | +/** |
| 185 | + A category on NSArray that provides a simple yet powerful interface for applying constraints to groups of views. |
| 186 | + */ |
| 187 | +@interface NSArray (AutoLayout) |
| 188 | + |
| 189 | +/** Aligns views in this array to one another along a given edge. */ |
| 190 | +- (NSArray *)autoAlignViewsToEdge:(ALEdge)edge; |
| 191 | + |
| 192 | +/** Aligns views in this array to one another along a given axis. */ |
| 193 | +- (NSArray *)autoAlignViewsToAxis:(ALAxis)axis; |
| 194 | + |
| 195 | +/** Matches a given dimension of all the views in this array. */ |
| 196 | +- (NSArray *)autoMatchViewsDimension:(ALDimension)dimension; |
| 197 | + |
| 198 | +/** Sets the given dimension of all the views in this array to a given size. */ |
| 199 | +- (NSArray *)autoSetViewsDimension:(ALDimension)dimension toSize:(CGFloat)size; |
| 200 | + |
| 201 | + |
| 202 | +/** Distributes the views in this array equally along the selected axis. Views will be the same size (variable) in the dimension along the axis and will have spacing (fixed) between them. */ |
| 203 | +- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSpacing:(CGFloat)spacing alignment:(NSLayoutFormatOptions)alignment; |
| 204 | + |
| 205 | +/** Distributes the views in this array equally along the selected axis. Views will be the same size (fixed) in the dimension along the axis and will have spacing (variable) between them. */ |
| 206 | +- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSize:(CGFloat)size alignment:(NSLayoutFormatOptions)alignment; |
| 207 | + |
| 208 | +@end |
| 209 | + |
| 210 | + |
| 211 | +#pragma mark - NSLayoutConstraint+AutoLayout |
| 212 | + |
| 213 | +/** |
| 214 | + A category on NSLayoutConstraint that allows constraints to be easily removed. |
| 215 | + */ |
| 216 | +@interface NSLayoutConstraint (AutoLayout) |
| 217 | + |
| 218 | +/** Removes the constraint from the view it has been added to. */ |
| 219 | +- (void)autoRemove; |
| 220 | + |
| 221 | +@end |
0 commit comments