diff --git a/Form Input Accessory View Demo.xcodeproj/project.pbxproj b/Form Input Accessory View Demo.xcodeproj/project.pbxproj index f99cfb2..858af2f 100644 --- a/Form Input Accessory View Demo.xcodeproj/project.pbxproj +++ b/Form Input Accessory View Demo.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 0117847C194EDF19007CA22F /* XCDButtonBarArrow.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 0117847B194EDF19007CA22F /* XCDButtonBarArrow.bundle */; }; C215427C164FD1C800262EB1 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = C2154275164FD1C800262EB1 /* Default-568h@2x.png */; }; C215427D164FD1C800262EB1 /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = C2154276164FD1C800262EB1 /* Default.png */; }; C215427E164FD1C800262EB1 /* Default@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = C2154277164FD1C800262EB1 /* Default@2x.png */; }; @@ -21,6 +22,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 0117847B194EDF19007CA22F /* XCDButtonBarArrow.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = XCDButtonBarArrow.bundle; sourceTree = ""; }; C2154275164FD1C800262EB1 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; C2154276164FD1C800262EB1 /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = ""; }; C2154277164FD1C800262EB1 /* Default@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default@2x.png"; sourceTree = ""; }; @@ -54,6 +56,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 0162EE941912A712002EF274 /* Resources */ = { + isa = PBXGroup; + children = ( + 0117847B194EDF19007CA22F /* XCDButtonBarArrow.bundle */, + ); + path = Resources; + sourceTree = ""; + }; C2F07133164DCA75008F4BA2 = { isa = PBXGroup; children = ( @@ -103,11 +113,13 @@ C2F07169164F0E1E008F4BA2 /* XCDFormInputAccessoryView */ = { isa = PBXGroup; children = ( + 0162EE941912A712002EF274 /* Resources */, C2F0716A164F0E32008F4BA2 /* XCDFormInputAccessoryView.h */, C2F0716B164F0E32008F4BA2 /* XCDFormInputAccessoryView.m */, ); path = XCDFormInputAccessoryView; sourceTree = ""; + usesTabs = 1; }; /* End PBXGroup section */ @@ -164,6 +176,7 @@ C215427D164FD1C800262EB1 /* Default.png in Resources */, C215427E164FD1C800262EB1 /* Default@2x.png in Resources */, C2154281164FD1C800262EB1 /* MainStoryboard.storyboard in Resources */, + 0117847C194EDF19007CA22F /* XCDButtonBarArrow.bundle in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -207,7 +220,7 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 5.0; + IPHONEOS_DEPLOYMENT_TARGET = 7.0; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; @@ -235,7 +248,7 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 5.0; + IPHONEOS_DEPLOYMENT_TARGET = 7.0; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; diff --git a/Form Input Accessory View Demo/DemoViewController.m b/Form Input Accessory View Demo/DemoViewController.m index 2ec0b18..82c85b3 100644 --- a/Form Input Accessory View Demo/DemoViewController.m +++ b/Form Input Accessory View Demo/DemoViewController.m @@ -64,7 +64,12 @@ - (UIView *) inputAccessoryView { if (!_inputAccessoryView) { - _inputAccessoryView = [[XCDFormInputAccessoryView alloc] initWithResponders:self.textInputs]; + // To use black colored toolbar + // [[UIToolbar appearance] setBarTintColor:[UIColor blackColor]]; + //_inputAccessoryView = [[XCDFormInputAccessoryView alloc] initWithResponders:self.textInputs tintColor:[UIColor whiteColor]]; + + _inputAccessoryView = [[XCDFormInputAccessoryView alloc] initWithResponders:self.textInputs]; + //_inputAccessoryView = [[XCDFormInputAccessoryView alloc] init]; //_inputAccessoryView = [[NSClassFromString(@"UIWebFormAccessory") alloc] init]; } diff --git a/README.md b/README.md index 07fd3c2..779d9ff 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ About ===== `XCDFormInputAccessoryView` is a view to be used above the keyboard with *previous*, *next* and *done* buttons for navigating text fields. -![screenshot](https://github.com/0xced/XCDFormInputAccessoryView/raw/master/Screenshots/XCDFormInputAccessoryView.png) +![screenshot](Screenshots/XCDFormInputAccessoryView.png) -`XCDFormInputAccessoryView` uses ARC (Automatic Reference Counting) and must be built with Xcode 4.5 or greater. It has been tested on iOS 4, 5 and 6. +`XCDFormInputAccessoryView` uses ARC (Automatic Reference Counting) and must be built with Xcode 4.5 or greater. Usage ===== @@ -38,4 +38,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Screenshots/XCDFormInputAccessoryView.png b/Screenshots/XCDFormInputAccessoryView.png index 788f9ab..46e7547 100644 Binary files a/Screenshots/XCDFormInputAccessoryView.png and b/Screenshots/XCDFormInputAccessoryView.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeft.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeft.png new file mode 100644 index 0000000..9ba3280 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeft.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeft@2x.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeft@2x.png new file mode 100644 index 0000000..c4a81c4 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeft@2x.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscape.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscape.png new file mode 100644 index 0000000..85403d4 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscape.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscape@2x.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscape@2x.png new file mode 100644 index 0000000..2665caa Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscape@2x.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscapeWhite.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscapeWhite.png new file mode 100644 index 0000000..4436a14 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscapeWhite.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscapeWhite@2x.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscapeWhite@2x.png new file mode 100644 index 0000000..50d31af Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscapeWhite@2x.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftWhite.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftWhite.png new file mode 100644 index 0000000..9e17b7b Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftWhite.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftWhite@2x.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftWhite@2x.png new file mode 100644 index 0000000..fd82634 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowLeftWhite@2x.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRight.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRight.png new file mode 100644 index 0000000..a4764ed Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRight.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRight@2x.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRight@2x.png new file mode 100644 index 0000000..3ec68e2 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRight@2x.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscape.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscape.png new file mode 100644 index 0000000..1cdd9a2 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscape.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscape@2x.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscape@2x.png new file mode 100644 index 0000000..93ecf5e Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscape@2x.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscapeWhite.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscapeWhite.png new file mode 100644 index 0000000..673c00a Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscapeWhite.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscapeWhite@2x.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscapeWhite@2x.png new file mode 100644 index 0000000..cf86842 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscapeWhite@2x.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightWhite.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightWhite.png new file mode 100644 index 0000000..e5633e1 Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightWhite.png differ diff --git a/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightWhite@2x.png b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightWhite@2x.png new file mode 100644 index 0000000..600513e Binary files /dev/null and b/XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle/UIButtonBarArrowRightWhite@2x.png differ diff --git a/XCDFormInputAccessoryView/XCDFormInputAccessoryView.h b/XCDFormInputAccessoryView/XCDFormInputAccessoryView.h index 0ba942e..f0c79d0 100644 --- a/XCDFormInputAccessoryView/XCDFormInputAccessoryView.h +++ b/XCDFormInputAccessoryView/XCDFormInputAccessoryView.h @@ -9,11 +9,21 @@ @interface XCDFormInputAccessoryView : UIView -- (id) initWithResponders:(NSArray *)responders; // Objects must be UIResponder instances +/** + * Objects must be UIResponder instances + */ + +- (instancetype) initWithResponders:(NSArray *)responders; + +- (instancetype) initWithResponders:(NSArray *)responders tintColor:(UIColor *)tintColor; @property (nonatomic, strong) NSArray *responders; -@property (nonatomic, assign) BOOL hasDoneButton; // Defaults to YES on iPhone, NO on iPad +/** + * Defaults to YES on iPhone, NO on iPad + */ + +@property (nonatomic, assign) BOOL hasDoneButton; - (void) setHasDoneButton:(BOOL)hasDoneButton animated:(BOOL)animated; diff --git a/XCDFormInputAccessoryView/XCDFormInputAccessoryView.m b/XCDFormInputAccessoryView/XCDFormInputAccessoryView.m index 06a1fc2..fd63a56 100644 --- a/XCDFormInputAccessoryView/XCDFormInputAccessoryView.m +++ b/XCDFormInputAccessoryView/XCDFormInputAccessoryView.m @@ -7,160 +7,210 @@ #import "XCDFormInputAccessoryView.h" -static NSString * UIKitLocalizedString(NSString *string) +static NSArray * EditableTextInputsInView(UIView *view) { - NSBundle *UIKitBundle = [NSBundle bundleForClass:[UIApplication class]]; - return UIKitBundle ? [UIKitBundle localizedStringForKey:string value:string table:nil] : string; + NSMutableArray *textInputs = [NSMutableArray new]; + for (UIView *subview in view.subviews) + { + BOOL isTextField = [subview isKindOfClass:[UITextField class]]; + BOOL isEditableTextView = [subview isKindOfClass:[UITextView class]] && [(UITextView *)subview isEditable]; + if (isTextField || isEditableTextView) + [textInputs addObject:subview]; + else + [textInputs addObjectsFromArray:EditableTextInputsInView(subview)]; + } + return textInputs; } -static NSArray * EditableTextInputsInView(UIView *view) +@interface XCDFormInputAccessoryView () + +@property (strong, nonatomic) UIBarButtonItem *previousBarButtonItem; +@property (strong, nonatomic) UIBarButtonItem *nextBarButtonItem; +@property (strong, nonatomic) UIToolbar *toolbar; + +@end + +@implementation XCDFormInputAccessoryView + +- (instancetype) initWithFrame:(CGRect)frame { - NSMutableArray *textInputs = [NSMutableArray new]; - for (UIView *subview in view.subviews) - { - BOOL isTextField = [subview isKindOfClass:[UITextField class]]; - BOOL isEditableTextView = [subview isKindOfClass:[UITextView class]] && [(UITextView *)subview isEditable]; - if (isTextField || isEditableTextView) - [textInputs addObject:subview]; - else - [textInputs addObjectsFromArray:EditableTextInputsInView(subview)]; - } - return textInputs; + return [self initWithResponders:nil]; } -@implementation XCDFormInputAccessoryView +- (id)initWithResponders:(NSArray *)responders +{ + return [self initWithResponders:responders tintColor:[UIColor blackColor]]; +} + +- (instancetype) initWithResponders:(NSArray *)responders tintColor:(UIColor *)tintColor { - UIToolbar *_toolbar; -} - -- (id) initWithFrame:(CGRect)frame -{ - return [self initWithResponders:nil]; -} - -- (id) initWithResponders:(NSArray *)responders -{ - if (!(self = [super initWithFrame:CGRectZero])) - return nil; - - _responders = responders; - - _toolbar = [[UIToolbar alloc] init]; - _toolbar.tintColor = nil; - _toolbar.barStyle = UIBarStyleBlack; - _toolbar.translucent = YES; - _toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth; - UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[ UIKitLocalizedString(@"Previous"), UIKitLocalizedString(@"Next") ]]; - [segmentedControl addTarget:self action:@selector(selectAdjacentResponder:) forControlEvents:UIControlEventValueChanged]; - segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; - segmentedControl.momentary = YES; - UIBarButtonItem *segmentedControlBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; - UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; - _toolbar.items = @[ segmentedControlBarButtonItem, flexibleSpace ]; - self.hasDoneButton = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone; - - [self addSubview:_toolbar]; - - self.frame = _toolbar.frame = (CGRect){CGPointZero, [_toolbar sizeThatFits:CGSizeZero]}; - - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil]; - - return self; + if (!(self = [super initWithFrame:CGRectZero])) + return nil; + + _responders = responders; + + self.toolbar = [[UIToolbar alloc] init]; + self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth; + self.toolbar.tintColor = tintColor; + + UIImage *previousImage = [UIImage imageNamed:@"XCDButtonBarArrow.bundle/UIButtonBarArrowLeft" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + UIImage *previousLandscapeImage = [UIImage imageNamed:@"XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscape" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + UIImage *nextImage = [UIImage imageNamed:@"XCDButtonBarArrow.bundle/UIButtonBarArrowRight" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + UIImage *nextLandscapeImage = [UIImage imageNamed:@"XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscape" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + + CGFloat whiteColorValue = 0.f; + [tintColor getWhite:&whiteColorValue alpha:NULL]; + if (whiteColorValue == 1.0) { + previousImage = [UIImage imageNamed:@"XCDButtonBarArrow.bundle/UIButtonBarArrowLeftWhite" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + previousLandscapeImage = [UIImage imageNamed:@"XCDButtonBarArrow.bundle/UIButtonBarArrowLeftLandscapeWhite" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + nextImage = [UIImage imageNamed:@"XCDButtonBarArrow.bundle/UIButtonBarArrowRightWhite" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + nextLandscapeImage = [UIImage imageNamed:@"XCDButtonBarArrow.bundle/UIButtonBarArrowRightLandscapeWhite" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + } + + UIBarButtonItem *previousBarButtonItem = [[UIBarButtonItem alloc] initWithImage:previousImage + landscapeImagePhone:previousLandscapeImage + style:UIBarButtonItemStylePlain + target:self + action:@selector(previous:)]; + self.previousBarButtonItem = previousBarButtonItem; + + UIBarButtonItem *nextBarButtonItem = [[UIBarButtonItem alloc] initWithImage:nextImage + landscapeImagePhone:nextLandscapeImage + style:UIBarButtonItemStylePlain + target:self + action:@selector(next:)]; + self.nextBarButtonItem = nextBarButtonItem; + + UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; + UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; + [fixedSpace setWidth:32]; + + self.toolbar.items = @[ self.previousBarButtonItem, fixedSpace, self.nextBarButtonItem, flexibleSpace ]; + [self addSubview:self.toolbar]; + + self.hasDoneButton = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone; + + self.frame = self.toolbar.frame = CGRectMake(0, 0, 0, 44); + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil]; + + return self; } - (void) dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; + [[NSNotificationCenter defaultCenter] removeObserver:self]; } -- (void) updateSegmentedControl +- (void) updateBarButtonItems { - NSArray *responders = self.responders; - if ([responders count] == 0) - return; - - UISegmentedControl *segmentedControl = (UISegmentedControl *)[_toolbar.items[0] customView]; - BOOL isFirst = [[responders objectAtIndex:0] isFirstResponder]; - BOOL isLast = [[responders lastObject] isFirstResponder]; - [segmentedControl setEnabled:!isFirst forSegmentAtIndex:0]; - [segmentedControl setEnabled:!isLast forSegmentAtIndex:1]; + NSArray *responders = self.responders; + if ([responders count] == 0) + return; + + BOOL isFirst = [[responders objectAtIndex:0] isFirstResponder]; + BOOL isLast = [[responders lastObject] isFirstResponder]; + [self.previousBarButtonItem setEnabled:!isFirst]; + [self.nextBarButtonItem setEnabled:!isLast]; } - (void) willMoveToWindow:(UIWindow *)window { - if (!window) - return; - - [self updateSegmentedControl]; + if (!window) + return; + + [self updateBarButtonItems]; } - (void) textInputDidBeginEditing:(NSNotification *)notification { - [self updateSegmentedControl]; + [self updateBarButtonItems]; } - (NSArray *) responders { - if (_responders) - return _responders; - - NSArray *textInputs = EditableTextInputsInView([[UIApplication sharedApplication] keyWindow]); - return [textInputs sortedArrayUsingComparator:^NSComparisonResult(UIView *textInput1, UIView *textInput2) { - UIView *commonAncestorView = textInput1.superview; - while (commonAncestorView && ![textInput2 isDescendantOfView:commonAncestorView]) - commonAncestorView = commonAncestorView.superview; - - CGRect frame1 = [textInput1 convertRect:textInput1.bounds toView:commonAncestorView]; - CGRect frame2 = [textInput2 convertRect:textInput2.bounds toView:commonAncestorView]; - return [@(CGRectGetMinY(frame1)) compare:@(CGRectGetMinY(frame2))]; - }]; + if (_responders) + return _responders; + + NSArray *textInputs = EditableTextInputsInView([[UIApplication sharedApplication] keyWindow]); + return [textInputs sortedArrayUsingComparator:^NSComparisonResult(UIView *textInput1, UIView *textInput2) { + UIView *commonAncestorView = textInput1.superview; + while (commonAncestorView && ![textInput2 isDescendantOfView:commonAncestorView]) + commonAncestorView = commonAncestorView.superview; + + CGRect frame1 = [textInput1 convertRect:textInput1.bounds toView:commonAncestorView]; + CGRect frame2 = [textInput2 convertRect:textInput2.bounds toView:commonAncestorView]; + return [@(CGRectGetMinY(frame1)) compare:@(CGRectGetMinY(frame2))]; + }]; +} + +- (UIResponder *) firstResponder +{ + NSArray *firstResponders = [self.responders filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIResponder *responder, NSDictionary *bindings) { + return [responder isFirstResponder]; + }]]; + UIResponder *firstResponder = [firstResponders lastObject]; + return firstResponder; } - (void) setHasDoneButton:(BOOL)hasDoneButton { - [self setHasDoneButton:hasDoneButton animated:NO]; + [self setHasDoneButton:hasDoneButton animated:NO]; } - (void) setHasDoneButton:(BOOL)hasDoneButton animated:(BOOL)animated { - if (_hasDoneButton == hasDoneButton) - return; - - [self willChangeValueForKey:@"hasDoneButton"]; - _hasDoneButton = hasDoneButton; - [self didChangeValueForKey:@"hasDoneButton"]; - - NSArray *items; - if (hasDoneButton) - items = [_toolbar.items arrayByAddingObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)]]; - else - items = [_toolbar.items subarrayWithRange:NSMakeRange(0, 2)]; - - [_toolbar setItems:items animated:animated]; + if (_hasDoneButton == hasDoneButton) + return; + + [self willChangeValueForKey:@"hasDoneButton"]; + _hasDoneButton = hasDoneButton; + [self didChangeValueForKey:@"hasDoneButton"]; + + NSArray *items; + if (hasDoneButton) + items = [self.toolbar.items arrayByAddingObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)]]; + else + items = [self.toolbar.items subarrayWithRange:NSMakeRange(0, 2)]; + + [self.toolbar setItems:items animated:animated]; } #pragma mark - Actions -- (void) selectAdjacentResponder:(UISegmentedControl *)sender +- (void) selectAdjacentResponderAtIndex:(NSInteger)index { - NSArray *firstResponders = [self.responders filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIResponder *responder, NSDictionary *bindings) { - return [responder isFirstResponder]; - }]]; - UIResponder *firstResponder = [firstResponders lastObject]; - NSInteger offset = sender.selectedSegmentIndex == 0 ? -1 : +1; - NSInteger firstResponderIndex = [self.responders indexOfObject:firstResponder]; - NSInteger adjacentResponderIndex = firstResponderIndex != NSNotFound ? firstResponderIndex + offset : NSNotFound; - UIResponder *adjacentResponder = nil; - if (adjacentResponderIndex >= 0 && adjacentResponderIndex < (NSInteger)[self.responders count]) - adjacentResponder = [self.responders objectAtIndex:adjacentResponderIndex]; - - [adjacentResponder becomeFirstResponder]; + UIResponder *firstResponder = [self firstResponder]; + NSInteger offset = index == 0 ? -1 : +1; + NSInteger firstResponderIndex = [self.responders indexOfObject:firstResponder]; + NSInteger adjacentResponderIndex = firstResponderIndex != NSNotFound ? firstResponderIndex + offset : NSNotFound; + UIResponder *adjacentResponder = nil; + if (adjacentResponderIndex >= 0 && adjacentResponderIndex < (NSInteger)[self.responders count]) + adjacentResponder = [self.responders objectAtIndex:adjacentResponderIndex]; + + // Resign the previous responder before selecting the next one, so the UIKeyboard events could be notified properly. + [firstResponder resignFirstResponder]; + + [adjacentResponder becomeFirstResponder]; +} + +- (void) previous:(UIBarButtonItem *)sender +{ + [self selectAdjacentResponderAtIndex:0]; +} + +- (void) next:(UIBarButtonItem *)sender +{ + [self selectAdjacentResponderAtIndex:1]; } - (void) done { - [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; + UIResponder *firstResponder = [self firstResponder]; + [firstResponder resignFirstResponder]; + + [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; } @end diff --git a/XCDFormInputAccessoryView7.podspec b/XCDFormInputAccessoryView7.podspec new file mode 100644 index 0000000..afe3012 --- /dev/null +++ b/XCDFormInputAccessoryView7.podspec @@ -0,0 +1,13 @@ +Pod::Spec.new do |s| + s.name = "XCDFormInputAccessoryView7" + s.version = "1.1.2" + s.summary = "Input accessory view with previous, next and done buttons updated to iOS 7 style." + s.homepage = "https://github.com/jessearmand/XCDFormInputAccessoryView" + s.license = { :type => 'MIT', :file => 'README.md' } + s.authors = { "Cédric Luthi" => "cedric.luthi@gmail.com", "Jesse Armand" => "jesse@jessearmand.com" } + s.source = { :git => "https://github.com/jessearmand/XCDFormInputAccessoryView.git", :tag => "1.1.2" } + s.platform = :ios, "7.0" + s.source_files = 'XCDFormInputAccessoryView' + s.resource = 'XCDFormInputAccessoryView/Resources/XCDButtonBarArrow.bundle' + s.requires_arc = true +end