Skip to content

Instantly share code, notes, and snippets.

@angelolloqui
Created June 5, 2013 13:51
Show Gist options
  • Save angelolloqui/5714010 to your computer and use it in GitHub Desktop.
Save angelolloqui/5714010 to your computer and use it in GitHub Desktop.
Simple accessoryview to deal with the Done button on keyboards with type UIKeyboardTypeNumberPad
#import <UIKit/UIKit.h>
@interface MOInputAccessoryView : UIButton
+ (MOInputAccessoryView *)inputAccessoryViewForTextField:(UITextField *)textfield;
@end
#import "MOInputAccessoryView.h"
@interface MOInputAccessoryView()
@property (nonatomic, weak) UITextField *textField;
@end
@implementation MOInputAccessoryView
+ (MOInputAccessoryView *)inputAccessoryViewForTextField:(UITextField *)textfield {
if (textfield.keyboardType != UIKeyboardTypeNumberPad) {
return nil;
}
MOInputAccessoryView *view = [MOInputAccessoryView buttonWithType:UIButtonTypeCustom];
view.textField = textfield;
[view addTarget:view action:@selector(endEditing) forControlEvents:UIControlEventTouchUpInside];
[view setTitle:NSLocalizedString(@"Return", nil) forState:UIControlStateNormal];
[view setTitleColor:[UIColor colorWithRed:77.0/255.0 green:84.0/255.0 blue:98.0/255.0 alpha:1] forState:UIControlStateNormal];
[view setTitleShadowColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateNormal];
view.titleLabel.font = [UIFont boldSystemFontOfSize:14];
view.titleLabel.shadowOffset = CGSizeMake(0, 1);
return view;
}
- (void)setFrame:(CGRect)frame {
CGRect keyboardFrame = self.superview.frame;
frame.size.height = 52;
frame.size.width = 105;
frame.origin.y = keyboardFrame.size.height - frame.size.height;
[super setFrame:frame];
}
- (void)endEditing {
[self.textField endEditing:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment