MASExampleRemakeView.m
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// MASExampleRemakeView.m
// Masonry iOS Examples
//
// Created by Sam Symons on 2014-06-22.
// Copyright (c) 2014 Jonas Budelmann. All rights reserved.
//
#import "MASExampleRemakeView.h"
@interface MASExampleRemakeView ()
@property (nonatomic, strong) UIButton *movingButton;
@property (nonatomic, assign) BOOL topLeft;
- (void)toggleButtonPosition;
@end
@implementation MASExampleRemakeView
- (id)init {
self = [super init];
if (!self) return nil;
self.movingButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.movingButton setTitle:@"Move Me!" forState:UIControlStateNormal];
self.movingButton.layer.borderColor = UIColor.greenColor.CGColor;
self.movingButton.layer.borderWidth = 3;
[self.movingButton addTarget:self action:@selector(toggleButtonPosition) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.movingButton];
self.topLeft = YES;
return self;
}
+ (BOOL)requiresConstraintBasedLayout
{
return YES;
}
// this is Apple's recommended place for adding/updating constraints
- (void)updateConstraints {
[self.movingButton remakeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@(100));
make.height.equalTo(@(100));
if (self.topLeft) {
make.left.equalTo(self.left).with.offset(10);
make.top.equalTo(self.top).with.offset(10);
}
else {
make.bottom.equalTo(self.bottom).with.offset(-10);
make.right.equalTo(self.right).with.offset(-10);
}
}];
//according to apple super should be called at end of method
[super updateConstraints];
}
- (void)toggleButtonPosition {
self.topLeft = !self.topLeft;
// tell constraints they need updating
[self setNeedsUpdateConstraints];
// update constraints now so we can animate the change
[self updateConstraintsIfNeeded];
[UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}];
}
@end