Wednesday, October 24, 2012

IPhone Custom Toolbar on Keyboard to Hide keyboard

IPhone Custom Toolbar on Keyboard

MY requirement was to work on numeric keys not alphabets so i have used Numeric Pad on UITEXTFIELD  and you all know that by default there is no button on numeric pad for hidden keyboard. You have to create Custom Button on ur numeric pad and call [numberTextField resignFirstResponder]; to hide numeric pad.

Solution

I have create a custom toolbar and have put two buttons on it 1) Apply and 2) Cancel. And created a method for both buttons.


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                           nil];
    [numberToolbar sizeToFit];
    numberTextField.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad{
    [numberTextField resignFirstResponder];
    numberTextField.text = @"";
}

-(void)doneWithNumberPad{
    NSString *numberFromTheKeyboard = numberTextField.text;
    [numberTextField resignFirstResponder];
}

Thats it .. 
You can add Custom button on left side of 0 button. 



No comments:

Post a Comment