Skip to content

Commit 9852374

Browse files
committed
Add some auto layout helper methods
1 parent 30635bd commit 9852374

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

Source/ALMenuCollectionViewCell.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#import "ALMenuCollectionViewCell.h"
99

10+
#import "UIView+ALLayout.h"
11+
1012
#import "ALMenuItem.h"
1113
#import "ALUtils.h"
1214

@@ -37,10 +39,7 @@ - (void)setButton:(UIView<ALMenuItem> *)button
3739
else
3840
{
3941
[self.contentView addSubview:button];
40-
[button.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor].active = YES;
41-
[button.topAnchor constraintEqualToAnchor:self.contentView.topAnchor].active = YES;
42-
[button.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor].active = YES;
43-
[button.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor].active = YES;
42+
[button al_pinToSuperview];
4443
}
4544

4645
_button = button;

Source/ALMenuViewController.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,7 @@ - (void)buildView
194194
[self.collectionView registerClass:[ALMenuCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuseIdentifier];
195195

196196
[self.containerView addSubview:self.collectionView];
197-
[self.collectionView.leadingAnchor constraintEqualToAnchor:self.containerView.leadingAnchor].active = YES;
198-
[self.collectionView.trailingAnchor constraintEqualToAnchor:self.containerView.trailingAnchor].active = YES;
199-
[self.collectionView.topAnchor constraintEqualToAnchor:self.containerView.topAnchor].active = YES;
200-
[self.collectionView.bottomAnchor constraintEqualToAnchor:self.containerView.bottomAnchor].active = YES;
197+
[self.collectionView al_pinToSuperview];
201198

202199
self.containerViewWidthConstraint = [self.containerView.widthAnchor constraintEqualToConstant:self.collectionView.contentSize.width];
203200
self.containerViewWidthConstraint.active = YES;

Source/UIView+ALLayout.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@
1313

1414
- (void)al_adjustPositionForNewAnchorPoint:(CGPoint)anchorPoint;
1515

16+
#pragma mark - Auto Layout
17+
18+
- (void)al_pinToSuperview;
19+
- (void)al_pinToView:(UIView *)view;
20+
1621
@end

Source/UIView+ALLayout.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,25 @@ - (void)al_adjustPositionForNewAnchorPoint:(CGPoint)anchorPoint
3030
self.layer.anchorPoint = anchorPoint;
3131
}
3232

33+
#pragma mark - Auto Layout
34+
35+
- (void)al_pinToSuperview
36+
{
37+
[self al_pinToView:self.superview];
38+
}
39+
40+
- (void)al_pinToView:(UIView *)view
41+
{
42+
NSParameterAssert(view != nil);
43+
44+
// forgetting to set this is the bane of my professional career
45+
self.translatesAutoresizingMaskIntoConstraints = NO;
46+
47+
// then constrain
48+
[self.leadingAnchor constraintEqualToAnchor:view.leadingAnchor].active = YES;
49+
[self.trailingAnchor constraintEqualToAnchor:view.trailingAnchor].active = YES;
50+
[self.topAnchor constraintEqualToAnchor:view.topAnchor].active = YES;
51+
[self.bottomAnchor constraintEqualToAnchor:view.bottomAnchor].active = YES;
52+
}
53+
3354
@end

0 commit comments

Comments
 (0)