Skip to content

Commit 0a1d687

Browse files
committed
Leverage new APIs to efficiently activate/deactivate arrays of constraints
- Use the class methods on NSLayoutConstraint to activate and deactivate arrays of constraints. These can be more efficient than activating/deactivating constraints individually. - Fix the preprocessor conditionals so that OS X 10.10 and higher can use the new constraint activation/deactivation APIs when available
1 parent fdf7cfe commit 0a1d687

10 files changed

Lines changed: 38 additions & 17 deletions

Example/PureLayoutTests/PureLayoutPriorityTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ - (void)testPriorityForPinningEdges
145145
}];
146146

147147
[self assertConstraintIsAddedWithDefaultPriorities:^NSLayoutConstraint *{
148-
return [weakSelf.viewB autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeft ofView:weakSelf.viewC withOffset:-95.0];
148+
return [weakSelf.viewB autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:weakSelf.viewC withOffset:-95.0];
149149
}];
150150

151151
[self assertConstraintIsAddedWithDefaultPriorities:^NSLayoutConstraint *{

Source/ALView+PureLayout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// ALView+PureLayout.h
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2012 Richard Turton

Source/ALView+PureLayout.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// ALView+PureLayout.m
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2012 Richard Turton

Source/NSArray+PureLayout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// NSArray+PureLayout.h
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2012 Richard Turton

Source/NSArray+PureLayout.m

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// NSArray+PureLayout.m
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2012 Richard Turton
@@ -45,6 +45,20 @@ @implementation NSArray (PureLayout)
4545
*/
4646
- (void)autoInstallConstraints
4747
{
48+
#if __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10
49+
if ([NSLayoutConstraint respondsToSelector:@selector(activateConstraints:)]) {
50+
for (id object in self) {
51+
if ([object isKindOfClass:[NSLayoutConstraint class]]) {
52+
[ALView al_applyGlobalStateToConstraint:object];
53+
}
54+
}
55+
if (![ALView al_preventAutomaticConstraintInstallation]) {
56+
[NSLayoutConstraint activateConstraints:self];
57+
}
58+
return;
59+
}
60+
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10 */
61+
4862
for (id object in self) {
4963
if ([object isKindOfClass:[NSLayoutConstraint class]]) {
5064
[((NSLayoutConstraint *)object) autoInstall];
@@ -57,6 +71,13 @@ - (void)autoInstallConstraints
5771
*/
5872
- (void)autoRemoveConstraints
5973
{
74+
#if __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10
75+
if ([NSLayoutConstraint respondsToSelector:@selector(deactivateConstraints:)]) {
76+
[NSLayoutConstraint deactivateConstraints:self];
77+
return;
78+
}
79+
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10 */
80+
6081
for (id object in self) {
6182
if ([object isKindOfClass:[NSLayoutConstraint class]]) {
6283
[((NSLayoutConstraint *)object) autoRemove];
@@ -359,10 +380,10 @@ - (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis
359380
return nil;
360381
}
361382
#if TARGET_OS_IPHONE
362-
BOOL isRightToLeftLayout = [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
383+
BOOL isRightToLeftLayout = [[UIApplication sharedApplication] userInterfaceLayoutDirection] == UIUserInterfaceLayoutDirectionRightToLeft;
363384
#else
364-
BOOL isRightToLeftLayout = [NSApp userInterfaceLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft;
365-
#endif
385+
BOOL isRightToLeftLayout = [[NSApplication sharedApplication] userInterfaceLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft;
386+
#endif /* TARGET_OS_IPHONE */
366387
BOOL shouldFlipOrder = isRightToLeftLayout && (axis != ALAxisVertical); // imitate the effect of leading/trailing when distributing horizontally
367388

368389
NSMutableArray *constraints = [NSMutableArray new];

Source/NSLayoutConstraint+PureLayout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// NSLayoutConstraint+PureLayout.h
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2013-2014 Tyler Fox

Source/NSLayoutConstraint+PureLayout.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// NSLayoutConstraint+PureLayout.m
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2013-2014 Tyler Fox
@@ -43,15 +43,15 @@ @implementation NSLayoutConstraint (PureLayout)
4343
*/
4444
- (void)autoInstall
4545
{
46-
#if __PureLayout_MinBaseSDK_iOS_8_0
46+
#if __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10
4747
if ([self respondsToSelector:@selector(setActive:)]) {
4848
[ALView al_applyGlobalStateToConstraint:self];
4949
if (![ALView al_preventAutomaticConstraintInstallation]) {
5050
self.active = YES;
5151
}
5252
return;
5353
}
54-
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
54+
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10 */
5555

5656
NSAssert(self.firstItem || self.secondItem, @"Can't install a constraint with nil firstItem and secondItem.");
5757
if (self.firstItem) {
@@ -74,12 +74,12 @@ - (void)autoInstall
7474
*/
7575
- (void)autoRemove
7676
{
77-
#if __PureLayout_MinBaseSDK_iOS_8_0
77+
#if __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10
7878
if ([self respondsToSelector:@selector(setActive:)]) {
7979
self.active = NO;
8080
return;
8181
}
82-
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
82+
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 || __PureLayout_MinBaseSDK_OSX_10_10 */
8383

8484
if (self.secondItem) {
8585
ALView *commonSuperview = [self.firstItem al_commonSuperviewWithView:self.secondItem];

Source/PureLayout+Internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// PureLayout+Internal.h
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2014 Tyler Fox

Source/PureLayout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// PureLayout.h
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2014 Tyler Fox

Source/PureLayoutDefines.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// PureLayoutDefines.h
3-
// v2.0.1
3+
// v2.0.2
44
// https://github.com/smileyborg/PureLayout
55
//
66
// Copyright (c) 2014 Tyler Fox

0 commit comments

Comments
 (0)