如何在键盘上建立一个栏

发布于 2024-08-28 14:38:31 字数 329 浏览 8 评论 0原文

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

I want to dismiss keyboard by click dismiss button ,How to build a bar on the keyboard? like this:
alt text
(source: alexcurylo.com)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绻影浮沉 2024-09-04 14:38:31

此处对此进行了解答。基本上,您向 UITextView 发送一条 resignFirstResponder 消息。当然,您可以将该代码放在按钮的委托上。

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.

短叹 2024-09-04 14:38:31

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

将以下变量放入 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;
}

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;
}
ㄖ落Θ余辉 2024-09-04 14:38:31

在这里你可以在键盘上建立一个栏
在xib上获取静态工具栏,然后将一个按钮放在工具栏上,然后将工具栏隐藏起来
当您创建工具栏时,在您的方法中编写代码,就像您想在文本视图上创建工具栏一样,然后开始编辑方法,

- (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];
}

希望这会对您有所帮助。

Here you can build a bar on a keyboard
Take static toolbar on xib and take one button and put it on toolbar, and make toolbar hidden ,now
write code in your method when you create a toolbar ,like if you want to create toolbar on textview begin editing method then,

- (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];
}

And when you dismiss keyboard write this method and call this method when toolbar button pressed

-(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];
}

Hope this will help you..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文