Skip to content

Commit 1583f7b

Browse files
committed
Move ALView class methods to NSLayoutConstraint
The following 4 class methods that currently exist on UIView/NSView make more sense on NSLayoutConstraint: * `+[autoCreateAndInstallConstraints:]` * `+[autoCreateConstraintsWithoutInstalling:]` * `+[autoSetPriority:forConstraints:]` * `+[autoSetIdentifier:forConstraints:]` This change makes the biggest improvement in Swift, when using the `autoSetPriority` or `autoSetIdentifier` methods with a trailing closure. This is a breaking API change, however all of these methods are simply moving, with no change in behavior. Additionally, some of the identifier methods were protected by the MinBaseSDK_iOS_8_0 macro, but weren’t also using the MinBaseSDK_OSX_10_10 macro, meaning these APIs would not be usable for a Mac target. This has been corrected.
1 parent 4c7e93b commit 1583f7b

File tree

7 files changed

+317
-326
lines changed

7 files changed

+317
-326
lines changed

PureLayout/PureLayout/ALView+PureLayout.h

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
__PL_ASSUME_NONNULL_BEGIN
3333

34-
#pragma mark - ALView+PureLayout
34+
#pragma mark ALView+PureLayout
3535

3636
/**
3737
A category on UIView/NSView that provides a simple yet powerful interface for creating Auto Layout constraints.
@@ -51,36 +51,6 @@ __PL_ASSUME_NONNULL_BEGIN
5151
- (instancetype)configureForAutoLayout;
5252

5353

54-
#pragma mark Batch Constraint Creation
55-
56-
/** Creates all of the constraints in the block, then installs (activates) them all at once.
57-
All constraints created from calls to the PureLayout API in the block are returned in a single array.
58-
This may be more efficient than installing (activating) each constraint one-by-one. */
59-
+ (__NSArray_of(NSLayoutConstraint *) *)autoCreateAndInstallConstraints:(ALConstraintsBlock)block;
60-
61-
/** Creates all of the constraints in the block but prevents them from being automatically installed (activated).
62-
All constraints created from calls to the PureLayout API in the block are returned in a single array. */
63-
+ (__NSArray_of(NSLayoutConstraint *) *)autoCreateConstraintsWithoutInstalling:(ALConstraintsBlock)block;
64-
65-
66-
#pragma mark Set Priority For Constraints
67-
68-
/** Sets the constraint priority to the given value for all constraints created using the PureLayout API within the given constraints block.
69-
NOTE: This method will have no effect (and will NOT set the priority) on constraints created or added without using the PureLayout API! */
70-
+ (void)autoSetPriority:(ALLayoutPriority)priority forConstraints:(ALConstraintsBlock)block;
71-
72-
73-
#pragma mark Set Identifier For Constraints
74-
75-
#if __PureLayout_MinBaseSDK_iOS_8_0
76-
77-
/** Sets the identifier for all constraints created using the PureLayout API within the given constraints block.
78-
NOTE: This method will have no effect (and will NOT set the identifier) on constraints created or added without using the PureLayout API! */
79-
+ (void)autoSetIdentifier:(NSString *)identifier forConstraints:(ALConstraintsBlock)block;
80-
81-
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
82-
83-
8454
#pragma mark Center & Align in Superview
8555

8656
/** Centers the view in its superview. */

PureLayout/PureLayout/ALView+PureLayout.m

Lines changed: 11 additions & 254 deletions
Original file line numberDiff line numberDiff line change
@@ -71,230 +71,6 @@ - (instancetype)configureForAutoLayout
7171
}
7272

7373

74-
#pragma mark Create Constraints Without Installing
75-
76-
/**
77-
A global variable that stores a stack of arrays of constraints created without being immediately installed.
78-
When executing a constraints block passed into the +[autoCreateConstraintsWithoutInstalling:] method, a new
79-
mutable array is pushed onto this stack, and all constraints created with PureLayout in the block are added
80-
to this array. When the block finishes executing, the array is popped off this stack. Automatic constraint
81-
installation is prevented if this stack contains at least 1 array.
82-
83-
NOTE: Access to this variable is not synchronized (and should only be done on the main thread).
84-
*/
85-
static __NSMutableArray_of(__NSMutableArray_of(NSLayoutConstraint *) *) *_al_arraysOfCreatedConstraints = nil;
86-
87-
/**
88-
A global variable that is set to YES when installing a batch of constraints collected from a call to +[autoCreateAndInstallConstraints].
89-
When this flag is YES, constraints are installed immediately without checking for or adding to the +[al_currentArrayOfCreatedConstraints].
90-
This is necessary to properly handle nested calls to +[autoCreateAndInstallConstraints], where calls whose block contains other call(s)
91-
should not return constraints from within the blocks of nested call(s).
92-
*/
93-
static BOOL _al_isInstallingCreatedConstraints = NO;
94-
95-
/**
96-
Accessor for the global state that stores arrays of constraints created without being installed.
97-
*/
98-
+ (__NSMutableArray_of(__NSMutableArray_of(NSLayoutConstraint *) *) *)al_arraysOfCreatedConstraints
99-
{
100-
if (!_al_arraysOfCreatedConstraints) {
101-
_al_arraysOfCreatedConstraints = [NSMutableArray new];
102-
}
103-
return _al_arraysOfCreatedConstraints;
104-
}
105-
106-
/**
107-
Accessor for the current mutable array of constraints created without being immediately installed.
108-
*/
109-
+ (__NSMutableArray_of(NSLayoutConstraint *) *)al_currentArrayOfCreatedConstraints
110-
{
111-
return [[self al_arraysOfCreatedConstraints] lastObject];
112-
}
113-
114-
/**
115-
Accessor for the global state that determines whether automatic constraint installation should be prevented.
116-
*/
117-
+ (BOOL)al_preventAutomaticConstraintInstallation
118-
{
119-
return (_al_isInstallingCreatedConstraints == NO) && ([[self al_arraysOfCreatedConstraints] count] > 0);
120-
}
121-
122-
/**
123-
Creates all of the constraints in the block, then installs (activates) them all at once.
124-
All constraints created from calls to the PureLayout API in the block are returned in a single array.
125-
This may be more efficient than installing (activating) each constraint one-by-one.
126-
127-
Note: calls to this method may be nested. The constraints returned from a call will NOT include constraints
128-
created in nested calls; constraints are only returned from the inner-most call they are created within.
129-
130-
@param block A block of method calls to the PureLayout API that create constraints.
131-
@return An array of the constraints that were created from calls to the PureLayout API inside the block.
132-
*/
133-
+ (__NSArray_of(NSLayoutConstraint *) *)autoCreateAndInstallConstraints:(ALConstraintsBlock)block
134-
{
135-
NSArray *createdConstraints = [self autoCreateConstraintsWithoutInstalling:block];
136-
_al_isInstallingCreatedConstraints = YES;
137-
[createdConstraints autoInstallConstraints];
138-
_al_isInstallingCreatedConstraints = NO;
139-
return createdConstraints;
140-
}
141-
142-
/**
143-
Creates all of the constraints in the block but prevents them from being automatically installed (activated).
144-
All constraints created from calls to the PureLayout API in the block are returned in a single array.
145-
146-
Note: calls to this method may be nested. The constraints returned from a call will NOT include constraints
147-
created in nested calls; constraints are only returned from the inner-most call they are created within.
148-
149-
@param block A block of method calls to the PureLayout API that create constraints.
150-
@return An array of the constraints that were created from calls to the PureLayout API inside the block.
151-
*/
152-
+ (__NSArray_of(NSLayoutConstraint *) *)autoCreateConstraintsWithoutInstalling:(ALConstraintsBlock)block
153-
{
154-
NSAssert(block, @"The constraints block cannot be nil.");
155-
NSArray *createdConstraints = nil;
156-
if (block) {
157-
[[self al_arraysOfCreatedConstraints] addObject:[NSMutableArray new]];
158-
block();
159-
createdConstraints = [self al_currentArrayOfCreatedConstraints];
160-
[[self al_arraysOfCreatedConstraints] removeLastObject];
161-
}
162-
return createdConstraints;
163-
}
164-
165-
166-
#pragma mark Set Priority For Constraints
167-
168-
/**
169-
A global variable that stores a stack of layout priorities to set on constraints.
170-
When executing a constraints block passed into the +[autoSetPriority:forConstraints:] method, the priority for
171-
that call is pushed onto this stack, and when the block finishes executing, that priority is popped off this
172-
stack. If this stack contains at least 1 priority, the priority at the top of the stack will be set for all
173-
constraints created by this library (even if automatic constraint installation is being prevented).
174-
NOTE: Access to this variable is not synchronized (and should only be done on the main thread).
175-
*/
176-
static __NSMutableArray_of(NSNumber *) *_al_globalConstraintPriorities = nil;
177-
178-
/**
179-
Accessor for the global stack of layout priorities.
180-
*/
181-
+ (__NSMutableArray_of(NSNumber *) *)al_globalConstraintPriorities
182-
{
183-
if (!_al_globalConstraintPriorities) {
184-
_al_globalConstraintPriorities = [NSMutableArray new];
185-
}
186-
return _al_globalConstraintPriorities;
187-
}
188-
189-
/**
190-
Returns the current layout priority to use for constraints.
191-
When executing a constraints block passed into +[autoSetPriority:forConstraints:], this will return
192-
the priority for the current block. Otherwise, the default Required priority is returned.
193-
*/
194-
+ (ALLayoutPriority)al_currentGlobalConstraintPriority
195-
{
196-
__NSMutableArray_of(NSNumber *) *globalConstraintPriorities = [self al_globalConstraintPriorities];
197-
if ([globalConstraintPriorities count] == 0) {
198-
return ALLayoutPriorityRequired;
199-
}
200-
return [[globalConstraintPriorities lastObject] floatValue];
201-
}
202-
203-
/**
204-
Accessor for the global state that determines if we're currently in the scope of a priority constraints block.
205-
*/
206-
+ (BOOL)al_isExecutingPriorityConstraintsBlock
207-
{
208-
return [[self al_globalConstraintPriorities] count] > 0;
209-
}
210-
211-
/**
212-
Sets the constraint priority to the given value for all constraints created using the PureLayout
213-
API within the given constraints block.
214-
215-
NOTE: This method will have no effect (and will NOT set the priority) on constraints created or added
216-
without using the PureLayout API!
217-
218-
@param priority The layout priority to be set on all constraints created in the constraints block.
219-
@param block A block of method calls to the PureLayout API that create and install constraints.
220-
*/
221-
+ (void)autoSetPriority:(ALLayoutPriority)priority forConstraints:(ALConstraintsBlock)block
222-
{
223-
NSAssert(block, @"The constraints block cannot be nil.");
224-
if (block) {
225-
[[self al_globalConstraintPriorities] addObject:@(priority)];
226-
block();
227-
[[self al_globalConstraintPriorities] removeLastObject];
228-
}
229-
}
230-
231-
232-
#pragma mark Set Identifier For Constraints
233-
234-
#if __PureLayout_MinBaseSDK_iOS_8_0
235-
236-
/**
237-
A global variable that stores a stack of identifier strings to set on constraints.
238-
When executing a constraints block passed into the +[autoSetIdentifier:forConstraints:] method, the identifier for
239-
that call is pushed onto this stack, and when the block finishes executing, that identifier is popped off this
240-
stack. If this stack contains at least 1 identifier, the identifier at the top of the stack will be set for all
241-
constraints created by this library (even if automatic constraint installation is being prevented).
242-
NOTE: Access to this variable is not synchronized (and should only be done on the main thread).
243-
*/
244-
static __NSMutableArray_of(NSString *) *_al_globalConstraintIdentifiers = nil;
245-
246-
/**
247-
Accessor for the global state of constraint identifiers.
248-
*/
249-
+ (__NSMutableArray_of(NSString *) *)al_globalConstraintIdentifiers
250-
{
251-
if (!_al_globalConstraintIdentifiers) {
252-
_al_globalConstraintIdentifiers = [NSMutableArray new];
253-
}
254-
return _al_globalConstraintIdentifiers;
255-
}
256-
257-
/**
258-
Returns the current identifier string to use for constraints.
259-
When executing a constraints block passed into +[autoSetIdentifier:forConstraints:], this will return
260-
the identifier for the current block. Otherwise, nil is returned.
261-
*/
262-
+ (NSString *)al_currentGlobalConstraintIdentifier
263-
{
264-
__NSMutableArray_of(NSString *) *globalConstraintIdentifiers = [self al_globalConstraintIdentifiers];
265-
if ([globalConstraintIdentifiers count] == 0) {
266-
return nil;
267-
}
268-
return [globalConstraintIdentifiers lastObject];
269-
}
270-
271-
/**
272-
Sets the identifier for all constraints created using the PureLayout API within the given constraints block.
273-
274-
NOTE: This method will have no effect (and will NOT set the identifier) on constraints created or added
275-
without using the PureLayout API!
276-
277-
@param identifier A string used to identify all constraints created in the constraints block.
278-
@param block A block of method calls to the PureLayout API that create and install constraints.
279-
*/
280-
+ (void)autoSetIdentifier:(NSString *)identifier forConstraints:(ALConstraintsBlock)block
281-
{
282-
NSAssert(block, @"The constraints block cannot be nil.");
283-
NSAssert(identifier, @"The identifier string cannot be nil.");
284-
if (block) {
285-
if (identifier) {
286-
[[self al_globalConstraintIdentifiers] addObject:identifier];
287-
}
288-
block();
289-
if (identifier) {
290-
[[self al_globalConstraintIdentifiers] removeLastObject];
291-
}
292-
}
293-
}
294-
295-
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
296-
297-
29874
#pragma mark Center in Superview
29975

30076
/**
@@ -756,14 +532,14 @@ - (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)
756532
*/
757533
- (void)autoSetContentCompressionResistancePriorityForAxis:(ALAxis)axis
758534
{
759-
NSAssert([ALView al_isExecutingPriorityConstraintsBlock], @"%@ should only be called from within the block passed into the method +[autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
760-
if ([ALView al_isExecutingPriorityConstraintsBlock]) {
535+
NSAssert([NSLayoutConstraint al_isExecutingPriorityConstraintsBlock], @"%@ should only be called from within the block passed into the method +[autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
536+
if ([NSLayoutConstraint al_isExecutingPriorityConstraintsBlock]) {
761537
self.translatesAutoresizingMaskIntoConstraints = NO;
762538
ALLayoutConstraintAxis constraintAxis = [NSLayoutConstraint al_constraintAxisForAxis:axis];
763539
#if TARGET_OS_IPHONE
764-
[self setContentCompressionResistancePriority:[ALView al_currentGlobalConstraintPriority] forAxis:constraintAxis];
540+
[self setContentCompressionResistancePriority:[NSLayoutConstraint al_currentGlobalConstraintPriority] forAxis:constraintAxis];
765541
#else
766-
[self setContentCompressionResistancePriority:[ALView al_currentGlobalConstraintPriority] forOrientation:constraintAxis];
542+
[self setContentCompressionResistancePriority:[NSLayoutConstraint al_currentGlobalConstraintPriority] forOrientation:constraintAxis];
767543
#endif /* TARGET_OS_IPHONE */
768544
}
769545
}
@@ -776,14 +552,14 @@ - (void)autoSetContentCompressionResistancePriorityForAxis:(ALAxis)axis
776552
*/
777553
- (void)autoSetContentHuggingPriorityForAxis:(ALAxis)axis
778554
{
779-
NSAssert([ALView al_isExecutingPriorityConstraintsBlock], @"%@ should only be called from within the block passed into the method +[autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
780-
if ([ALView al_isExecutingPriorityConstraintsBlock]) {
555+
NSAssert([NSLayoutConstraint al_isExecutingPriorityConstraintsBlock], @"%@ should only be called from within the block passed into the method +[autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
556+
if ([NSLayoutConstraint al_isExecutingPriorityConstraintsBlock]) {
781557
self.translatesAutoresizingMaskIntoConstraints = NO;
782558
ALLayoutConstraintAxis constraintAxis = [NSLayoutConstraint al_constraintAxisForAxis:axis];
783559
#if TARGET_OS_IPHONE
784-
[self setContentHuggingPriority:[ALView al_currentGlobalConstraintPriority] forAxis:constraintAxis];
560+
[self setContentHuggingPriority:[NSLayoutConstraint al_currentGlobalConstraintPriority] forAxis:constraintAxis];
785561
#else
786-
[self setContentHuggingPriority:[ALView al_currentGlobalConstraintPriority] forOrientation:constraintAxis];
562+
[self setContentHuggingPriority:[NSLayoutConstraint al_currentGlobalConstraintPriority] forOrientation:constraintAxis];
787563
#endif /* TARGET_OS_IPHONE */
788564
}
789565
}
@@ -948,25 +724,6 @@ - (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewContro
948724

949725
#pragma mark Internal Methods
950726

951-
/**
952-
Applies the global constraint priority and identifier to the given constraint.
953-
This should be done before installing all constraints.
954-
955-
@param constraint The constraint to set the global priority and identifier on.
956-
*/
957-
+ (void)al_applyGlobalStateToConstraint:(NSLayoutConstraint *)constraint
958-
{
959-
if ([ALView al_isExecutingPriorityConstraintsBlock]) {
960-
constraint.priority = [ALView al_currentGlobalConstraintPriority];
961-
}
962-
#if __PureLayout_MinBaseSDK_iOS_8_0
963-
NSString *globalConstraintIdentifier = [ALView al_currentGlobalConstraintIdentifier];
964-
if (globalConstraintIdentifier) {
965-
[constraint autoIdentify:globalConstraintIdentifier];
966-
}
967-
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
968-
}
969-
970727
/**
971728
Adds the given constraint to this view after applying the global state to the constraint.
972729
NOTE: This method is compatible with all versions of iOS, and should be used for older versions before the active
@@ -978,9 +735,9 @@ + (void)al_applyGlobalStateToConstraint:(NSLayoutConstraint *)constraint
978735
*/
979736
- (void)al_addConstraint:(NSLayoutConstraint *)constraint
980737
{
981-
[ALView al_applyGlobalStateToConstraint:constraint];
982-
if ([ALView al_preventAutomaticConstraintInstallation]) {
983-
[[ALView al_currentArrayOfCreatedConstraints] addObject:constraint];
738+
[NSLayoutConstraint al_applyGlobalStateToConstraint:constraint];
739+
if ([NSLayoutConstraint al_preventAutomaticConstraintInstallation]) {
740+
[[NSLayoutConstraint al_currentArrayOfCreatedConstraints] addObject:constraint];
984741
} else {
985742
[self addConstraint:constraint];
986743
}

PureLayout/PureLayout/NSArray+PureLayout.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
__PL_ASSUME_NONNULL_BEGIN
3333

34-
#pragma mark - NSArray+PureLayout
34+
#pragma mark NSArray+PureLayout
3535

3636
/**
3737
A category on NSArray that provides a simple yet powerful interface to:
@@ -49,12 +49,12 @@ __PL_ASSUME_NONNULL_BEGIN
4949
/** Deactivates the constraints in this array. */
5050
- (void)autoRemoveConstraints;
5151

52-
#if __PureLayout_MinBaseSDK_iOS_8_0
52+
#if __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10
5353

5454
/** Sets the string as the identifier for the constraints in this array. Available in iOS 7.0 and OS X 10.9 and later. */
5555
- (instancetype)autoIdentifyConstraints:(NSString *)identifier;
5656

57-
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
57+
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10 */
5858

5959

6060
#pragma mark Array of Views

0 commit comments

Comments
 (0)