0

我想通过单击关闭按钮来关闭键盘,如何在键盘上建立一个栏?像这样:( 来源:alexcurylo.com替代文字

4

3 回答 3

6

它在这里得到了回答。基本上,您resignFirstResponderUITextView. 当然,您会将该代码放在按钮的委托上。

于 2010-04-11T13:32:52.740 回答
1

尝试输入以下代码。它可能对你有用。

将以下变量放入 viewController.h 文件中

UIToolbar *keyboardToolbar;

    id notisender;

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

现在将以下代码放入您的 viewController.m 文件中。

- (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;
}
于 2010-04-13T15:14:25.663 回答
1

在这里您可以在键盘上构建一个栏在xib上获取静态工具栏并将一个按钮放在工具栏上,并使工具栏隐藏,现在在您创建工具栏时在您的方法中编写代码,就像您想在textview上创建工具栏一样然后开始编辑方法,

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.26];
    [tlBar setFrame:CGRectMake(0, 220, 320, 44)];
    tlBar.hidden=NO;
    [UIView commitAnimations];
}

当您关闭键盘时,编写此方法并在按下工具栏按钮时调用此方法

-(IBAction)kbAway:(id)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    [tlBar setFrame:CGRectMake(0, 480, 320, 44)];
    [UIView commitAnimations];
    [txtviewmessage resignFirstResponder];
    [txtsubject resignFirstResponder];
    [txttemplatename resignFirstResponder];
}

希望对你有帮助..

于 2012-05-18T07:55:49.297 回答