tags:

views:

207

answers:

2

I want to dismiss keyboard by click dismiss button ,How to build a bar on the keyboard? like this: alt text

+5  A: 

It was answered here. Basically, you send a resignFirstResponder message to the UITextView. Of course, you will put that code on your button's delegate.

Pablo Santa Cruz
+1  A: 

Try to put following code. It might work for you.

Put following variables in your viewController.h file

UIToolbar *keyboardToolbar;

    id notisender;

    BOOL isAlreadyResigned;
    float kx,ky,kh,kw;

Now put following code in you viewController.m file.

- (void)viewDidLoad {
    [super viewDidLoad];

}

-(void)viewWillAppear:(BOOL)animated{
    isAlreadyResigned=YES;
    [self keyboardToolbarShouldShow];
    [self keyboardToolbarShouldShow];
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [self keyboardToolbarShouldNotShow];
    [super viewWillDisappear:animated];
}



#pragma mark keyboardWillShow methods
-(void)keyboardToolbarShouldShow {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardhide:) 
                                                 name:UIKeyboardWillHideNotification 
                                               object:nil];
}

-(void)keyboardToolbarShouldNotShow {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification 
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification 
                                                  object:nil];
    if(!isAlreadyResigned){
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillNotShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
        [GEEtxtMsg becomeFirstResponder];
    }
}

-(void)keyboardWillShow : (NSNotification *)sender {
    @try  {
//      NSLog(@"notification in first view");
        for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) { 
            for (UIView *keyboard in [keyboardWindow subviews]) {
                NSLog(@"keyboard description - %@",[keyboard description]);
                if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
                    NSValue *v = [[sender userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
                    CGRect kbBounds = [v CGRectValue];
                    if(keyboardToolbar == nil) {
                        keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectZero];
                        keyboardToolbar.barStyle=UIBarStyleBlackOpaque;
                        keyboardToolbar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
                        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(dismissKeyboard)];
                        UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
                        NSArray *items = [[NSArray alloc] initWithObjects:flex, barButtonItem, nil];
                        [keyboardToolbar setItems:items];
                        [items release];
                    }               
                    [keyboardToolbar removeFromSuperview];
                    keyboardToolbar.frame = CGRectMake(0, 0, kbBounds.size.width, 45);
                    [keyboard addSubview:keyboardToolbar];
                    NSLog(@"x=%f y=%f width=%f height=%f",kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height);
                    kx=kbBounds.origin.x; ky=kbBounds.origin.y;
                    kh=kbBounds.size.height; kw=kbBounds.size.width;
                    keyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height + 87);
                    isAlreadyResigned=NO;
                    for(UIView* subKeyboard in [keyboard subviews]) {
                        if([[subKeyboard description] hasPrefix:@"<UIKeyboardImpl"] == YES) {
                            subKeyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y - 45, kbBounds.size.width, kbBounds.size.height);  
                        }                       
                    }
                }
            }
        }
    } @catch (NSException * e) {
        NSLog(@"Problem in keyboardWillShow:%@",e);
    }
}

-(void)keyboardWillNotShow : (NSNotification *)sender {
    @try  {
        for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
            for (UIView *keyboard in [keyboardWindow subviews]) {
                if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
//                  NSValue *v = [[sender userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
//                  CGRect kbBounds = [v CGRectValue];
                    if([[keyboard subviews] containsObject:keyboardToolbar]){
                        [keyboardToolbar removeFromSuperview];
                    }
                    if(!isAlreadyResigned){
                        isAlreadyResigned=YES;
                        keyboard.bounds = CGRectMake(kx,ky,kw,kh);
                        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                        name:UIKeyboardWillShowNotification 
                                                                      object:nil];
                        for(UIView* subKeyboard in [keyboard subviews]) {
                            if([[subKeyboard description] hasPrefix:@"<UIKeyboardImpl"] == YES) {
                                subKeyboard.bounds = CGRectMake(0, 0,0, 0); 
                            }                       
                        }
                    }
                }
            }
        }
    } @catch (NSException * e) {
        NSLog(@"Problem in keyboardWillShow:%@",e);
    }
}

-(void)dismissKeyboard {    
    [self resignTextFields];
}

-(void)keyboardhide:(NSNotification *)noti {
    notisender = noti;
}
sugar
You are my hero,thank you very much!!!
HelloWorld