Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Støvring committed Mar 10, 2014
2 parents 2db3939 + 142e133 commit 0a2809b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
8 changes: 4 additions & 4 deletions BSKeyboardControls/BSKeyboardControls.h
Expand Up @@ -29,7 +29,7 @@ typedef enum

@protocol BSKeyboardControlsDelegate;

@interface BSKeyboardControls : UIView
@interface BSKeyboardControls : UIView <UIInputViewAudioFeedback>

/**
* Delegate to send callbacks to.
Expand Down Expand Up @@ -68,17 +68,17 @@ typedef enum
@property (nonatomic, strong) UIColor *barTintColor;

/**
* Tint color of the segmented control.
* Tint color of the segmented control. iOS 6 Only.
*/
@property (nonatomic, strong) UIColor *segmentedControlTintControl;

/**
* Title of the previous button. If this is not set, a default localized title will be used.
* Title of the previous button. If this is not set, a default localized title will be used. iOS 6 Only.
*/
@property (nonatomic, strong) NSString *previousTitle;

/**
* Title of the next button. If this is not set, a default localized title will be used.
* Title of the next button. If this is not set, a default localized title will be used. iOS 6 Only.
*/
@property (nonatomic, strong) NSString *nextTitle;

Expand Down
71 changes: 55 additions & 16 deletions BSKeyboardControls/BSKeyboardControls.m
Expand Up @@ -11,12 +11,20 @@
@interface BSKeyboardControls ()
@property (nonatomic, strong) UIToolbar *toolbar;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) UIBarButtonItem *leftArrowButton;
@property (nonatomic, strong) UIBarButtonItem *rightArrowButton;
@property (nonatomic, strong) UIBarButtonItem *doneButton;
@property (nonatomic, strong) UIBarButtonItem *segmentedControlItem;
@end

@implementation BSKeyboardControls


- (BOOL) enableInputClicksWhenVisible {
return YES;
}


#pragma mark -
#pragma mark Lifecycle

Expand All @@ -35,18 +43,28 @@ - (id)initWithFields:(NSArray *)fields
if (self = [super initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)])
{
[self setToolbar:[[UIToolbar alloc] initWithFrame:self.frame]];
[self.toolbar setBarStyle:UIBarStyleBlackTranslucent];
[self.toolbar setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth)];
[self addSubview:self.toolbar];

[self setSegmentedControl:[[UISegmentedControl alloc] initWithItems:@[ NSLocalizedStringFromTable(@"Previous", @"BSKeyboardControls", @"Previous button title."),
NSLocalizedStringFromTable(@"Next", @"BSKeyboardControls", @"Next button title.") ]]];
[self.segmentedControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.segmentedControl setMomentary:YES];
[self.segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.segmentedControl setEnabled:NO forSegmentAtIndex:BSKeyboardControlsDirectionPrevious];
[self.segmentedControl setEnabled:NO forSegmentAtIndex:BSKeyboardControlsDirectionNext];
[self setSegmentedControlItem:[[UIBarButtonItem alloc] initWithCustomView:self.segmentedControl]];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[self setLeftArrowButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:self action:@selector(selectPreviousField)]];
[self.leftArrowButton setEnabled:NO];
[self.rightArrowButton setEnabled:NO];
[self setRightArrowButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:self action:@selector(selectNextField)]];

} else {
[self setBarStyle:UIBarStyleBlackTranslucent];

[self setSegmentedControl:[[UISegmentedControl alloc] initWithItems:@[ NSLocalizedStringFromTable(@"Previous", @"BSKeyboardControls", @"Previous button title."),
NSLocalizedStringFromTable(@"Next", @"BSKeyboardControls", @"Next button title.") ]]];
[self.segmentedControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];

[self.segmentedControl setMomentary:YES];
[self.segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.segmentedControl setEnabled:NO forSegmentAtIndex:BSKeyboardControlsDirectionPrevious];
[self.segmentedControl setEnabled:NO forSegmentAtIndex:BSKeyboardControlsDirectionNext];
[self setSegmentedControlItem:[[UIBarButtonItem alloc] initWithCustomView:self.segmentedControl]];
}

[self setDoneButton:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(@"Done", @"BSKeyboardControls", @"Done button title.")
style:UIBarButtonItemStyleDone
Expand All @@ -72,6 +90,8 @@ - (void)dealloc
[self setDoneTintColor:nil];
[self setActiveField:nil];
[self setToolbar:nil];
[self setRightArrowButton:nil];
[self setLeftArrowButton:nil];
[self setSegmentedControl:nil];
[self setSegmentedControlItem:nil];
[self setDoneButton:nil];
Expand All @@ -95,7 +115,7 @@ - (void)setActiveField:(id)activeField
[activeField becomeFirstResponder];
}

[self updateSegmentedControlEnabledStates];
[self updatePrevoidNextEnabledStates];
}
}
}
Expand Down Expand Up @@ -206,6 +226,8 @@ - (void)setVisibleControls:(BSKeyboardControl)visibleControls

- (void)segmentedControlValueChanged:(id)sender
{
[[UIDevice currentDevice] playInputClick];

switch (self.segmentedControl.selectedSegmentIndex)
{
case BSKeyboardControlsDirectionPrevious:
Expand All @@ -227,13 +249,18 @@ - (void)doneButtonPressed:(id)sender
}
}

- (void)updateSegmentedControlEnabledStates
- (void)updatePrevoidNextEnabledStates
{
NSInteger index = [self.fields indexOfObject:self.activeField];
if (index != NSNotFound)
{
[self.segmentedControl setEnabled:(index > 0) forSegmentAtIndex:BSKeyboardControlsDirectionPrevious];
[self.segmentedControl setEnabled:(index < [self.fields count] - 1) forSegmentAtIndex:BSKeyboardControlsDirectionNext];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[self.leftArrowButton setEnabled:(index > 0)];
[self.rightArrowButton setEnabled:(index < [self.fields count] - 1)];
} else {
[self.segmentedControl setEnabled:(index > 0) forSegmentAtIndex:BSKeyboardControlsDirectionPrevious];
[self.segmentedControl setEnabled:(index < [self.fields count] - 1) forSegmentAtIndex:BSKeyboardControlsDirectionNext];
}
}
}

Expand Down Expand Up @@ -272,9 +299,21 @@ - (void)selectNextField
- (NSArray *)toolbarItems
{
NSMutableArray *items = [NSMutableArray arrayWithCapacity:3];
if (self.visibleControls & BSKeyboardControlPreviousNext)
{
[items addObject:self.segmentedControlItem];

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
if (self.visibleControls & BSKeyboardControlPreviousNext)
{
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpace.width = 22.0;
[items addObjectsFromArray:@[self.leftArrowButton,
fixedSpace,
self.rightArrowButton]];
}
} else {
if (self.visibleControls & BSKeyboardControlPreviousNext)
{
[items addObject:self.segmentedControlItem];
}
}

if (self.visibleControls & BSKeyboardControlDone)
Expand Down
Binary file not shown.

0 comments on commit 0a2809b

Please sign in to comment.