-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathSWUtilityButtonView.m
executable file
·153 lines (117 loc) · 5.82 KB
/
SWUtilityButtonView.m
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// SWUtilityButtonView.m
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "SWUtilityButtonView.h"
#import "SWUtilityButtonTapGestureRecognizer.h"
@interface SWUtilityButtonView()
@property (nonatomic, strong) NSLayoutConstraint *widthConstraint;
@property (nonatomic, strong) NSMutableArray *buttonBackgroundColors;
@end
@implementation SWUtilityButtonView
#pragma mark - SWUtilityButonView initializers
- (id)initWithUtilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector
{
self = [self initWithFrame:CGRectZero utilityButtons:utilityButtons parentCell:parentCell utilityButtonSelector:utilityButtonSelector];
return self;
}
- (id)initWithFrame:(CGRect)frame utilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector
{
self = [super initWithFrame:frame];
if (self) {
self.translatesAutoresizingMaskIntoConstraints = NO;
self.widthConstraint = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:0.0]; // constant will be adjusted dynamically in -setUtilityButtons:.
self.widthConstraint.priority = UILayoutPriorityDefaultHigh;
[self addConstraint:self.widthConstraint];
_parentCell = parentCell;
self.utilityButtonSelector = utilityButtonSelector;
self.utilityButtons = utilityButtons;
}
return self;
}
#pragma mark Populating utility buttons
- (void)setUtilityButtons:(NSArray *)utilityButtons
{
// if no width specified, use the default width
[self setUtilityButtons:utilityButtons WithButtonWidth:kUtilityButtonWidthDefault];
}
- (void)setUtilityButtons:(NSArray *)utilityButtons WithButtonWidth:(CGFloat)width
{
for (UIButton *button in _utilityButtons)
{
[button removeFromSuperview];
}
_utilityButtons = [utilityButtons copy];
if (utilityButtons.count)
{
NSUInteger utilityButtonsCounter = 0;
UIView *precedingView = nil;
for (UIButton *button in _utilityButtons)
{
[self addSubview:button];
button.translatesAutoresizingMaskIntoConstraints = NO;
if (!precedingView)
{
// First button; pin it to the left edge.
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button]"
options:0L
metrics:nil
views:NSDictionaryOfVariableBindings(button)]];
}
else
{
// Subsequent button; pin it to the right edge of the preceding one, with equal width.
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[precedingView][button(==precedingView)]"
options:0L
metrics:nil
views:NSDictionaryOfVariableBindings(precedingView, button)]];
}
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|"
options:0L
metrics:nil
views:NSDictionaryOfVariableBindings(button)]];
SWUtilityButtonTapGestureRecognizer *utilityButtonTapGestureRecognizer = [[SWUtilityButtonTapGestureRecognizer alloc] initWithTarget:_parentCell action:_utilityButtonSelector];
utilityButtonTapGestureRecognizer.buttonIndex = utilityButtonsCounter;
[button addGestureRecognizer:utilityButtonTapGestureRecognizer];
utilityButtonsCounter++;
precedingView = button;
}
// Pin the last button to the right edge.
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[precedingView]|"
options:0L
metrics:nil
views:NSDictionaryOfVariableBindings(precedingView)]];
}
self.widthConstraint.constant = (width * utilityButtons.count);
[self setNeedsLayout];
return;
}
#pragma mark -
- (void)pushBackgroundColors
{
self.buttonBackgroundColors = [[NSMutableArray alloc] init];
for (UIButton *button in self.utilityButtons)
{
[self.buttonBackgroundColors addObject:button.backgroundColor];
}
}
- (void)popBackgroundColors
{
NSEnumerator *e = self.utilityButtons.objectEnumerator;
for (UIColor *color in self.buttonBackgroundColors)
{
UIButton *button = [e nextObject];
button.backgroundColor = color;
}
self.buttonBackgroundColors = nil;
}
@end