Tuesday 16 October 2012

Set toolbar for keyboard


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardDidShow:) 
                                                 name:UIKeyboardDidShowNotification 
                                               object:nil];
    return YES;
}

- (void)keyboardDidShow:(NSNotification *)note
{
    UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    returnBtn.frame = CGRectMake(0,-25,320,25);
    returnBtn.adjustsImageWhenHighlighted = NO;
    returnBtn.backgroundColor=[UIColor darkGrayColor];
    returnBtn.titleLabel.textColor=[UIColor whiteColor];
    [returnBtn setBackgroundImage:[UIImage imageNamed:@"keyBtn.png"] forState:UIControlStateNormal];
    
    [returnBtn addTarget:self action:@selector(keyboardBtn:) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
            //  if (txtTag==5) {
            [keyboard addSubview:returnBtn];
    }
}

-(IBAction)keyboardBtn:(id)sender
{
    [yourTextField resignFirstResponder];/// resign all textfield
}

No comments:

Post a Comment