-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIDefaultAccessoryInputView.m
More file actions
139 lines (110 loc) · 4.58 KB
/
Copy pathUIDefaultAccessoryInputView.m
File metadata and controls
139 lines (110 loc) · 4.58 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
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
//
// UIDefaultAccessoryInputView.m
// Created by Bryan Dunbar on 2/4/13.
//
#import "UIDefaultAccessoryInputView.h"
@interface UIDefaultAccessoryInputView ()
-(void)configureButtons;
@end
@implementation UIDefaultAccessoryInputView
@synthesize nextPrevSegment=_nextPrevSegment;
@synthesize accessoryInputViewDelegate=_accessoryInputViewDelegate;
@synthesize extraButtons=_extraButtons;
@synthesize showsDoneButton=_showsDoneButton;
@synthesize showsNextPrev=_showsNextPrev;
-(void)setShowsDoneButton:(BOOL)showsDoneButton {
if (_showsDoneButton != showsDoneButton) {
_showsDoneButton = showsDoneButton;
[self configureButtons];
}
}
-(void)setShowsNextPrev:(BOOL)showsNextPrev {
if (_showsNextPrev != showsNextPrev) {
_showsNextPrev = showsNextPrev;
[self configureButtons];
}
}
-(UISegmentedControl*)nextPrevSegment {
if (!_nextPrevSegment) {
_nextPrevSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
//_nextPrevSegment.segmentedControlStyle = UISegmentedControlStyleBar;
[_nextPrevSegment addTarget:self action:@selector(nextPrevTapped:) forControlEvents:UIControlEventValueChanged];
_nextPrevSegment.momentary = YES;
}
return _nextPrevSegment;
}
-(id)init {
return [self initWithHostView:nil];
}
-(id)initWithHostView:(UIView *)hostView {
return [self initWithHostView:hostView andExtraButtons:nil];
}
-(id)initWithHostView:(UIView *)hostView andExtraButtons:(NSArray *)extraButtons {
if (self = [super init]) {
self.hostView = hostView;
_extraButtons = extraButtons;
//self.barStyle = UIBarStyleBlack;
//self.barStyle = UIBarStyleBlackTranslucent;
self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self sizeToFit];
_showsDoneButton = YES;
CGRect frame = self.frame;
frame.size.height = 44.0f;
self.frame = frame;
[self configureButtons];
}
return self;
}
-(void)configureButtons {
NSDictionary* textAttributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:16]
};
UIBarButtonItem *nextPrevBtn = nil;
if (self.showsNextPrev) {
nextPrevBtn = [[UIBarButtonItem alloc] initWithCustomView:self.nextPrevSegment];
[nextPrevBtn setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
}
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *flexibleSpaceright = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
//[doneBtn setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
NSMutableArray *array = [NSMutableArray array];
if (nextPrevBtn) {
[array addObjectsFromArray:@[nextPrevBtn, flexibleSpaceLeft]];
}
[array addObject:flexibleSpaceright];
if (self.showsDoneButton) {
UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
[array addObject:doneBtn];
} else {
// Have to have something here to stop the flexible space
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = 1;
[array addObject:space];
}
// Add any extra buttons
for (int i = self.extraButtons.count - 1; i >= 0; i--) {
UIBarButtonItem *extraButton = [self.extraButtons objectAtIndex:i];
[extraButton setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
[array insertObject:extraButton atIndex:1]; // After the segment control
}
self.items = nil;
[self setItems:array];
[self setNeedsDisplay];
}
-(void)setExtraButtons:(NSArray *)extraButtons {
_extraButtons = extraButtons;
[self configureButtons];
}
-(void)nextPrevTapped:(UISegmentedControl *)sender {
if ([self.accessoryInputViewDelegate respondsToSelector:@selector(nextPrevTapped:)]) {
[self.accessoryInputViewDelegate nextPrevTapped:self.nextPrevSegment];
}
}
-(void)done:(id)sender {
if ([self.accessoryInputViewDelegate respondsToSelector:@selector(doneTapped:)]) {
[self.accessoryInputViewDelegate doneTapped:self];
}
// Resign the responder as we are done with it
[self.hostView resignFirstResponder];
}
@end