键盘顶部的工具栏和firstresponder

发布于 2024-09-08 09:00:06 字数 2069 浏览 3 评论 0原文

这是一个非常常见的话题,并且有很多答案。

情况:我有一个全屏(不包括工具栏)UITextView,底部有一个UIToolbar。当 UITextView 获得第一响应者时,我想让工具栏随键盘向上滑动,并添加一个“完成”按钮,该按钮将关闭键盘。

到目前为止:我已经完全正常工作了,基于这个例子。除了当我将 [textViewBecomeFirstResponder]; 放入 viewDidLoad 中时,工具栏不会显示动画。即使 keyboardWillShow 被调用。有人知道吗?

代码:这样您就不必查看示例代码,这就是发生的情况:

在viewDidLoad中:

- (void)viewDidLoad {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
   [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
   [nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
   [textView becomeFirstResponder];
       [super viewDidLoad];
}

在keyboardWillShow中:

- (void)keyboardWillShow:(NSNotification *)notification {
NSLog(@"keyboard will show");
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                                            target:self 
                                                                            action:@selector(keyboardDone)];
    NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:[toolbar items]];
    [toolbarItems addObject:doneButton];
    [toolbar setItems:toolbarItems];

    CGRect frame = self.view.frame;
    frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
    self.view.frame = frame;
     [UIView commitAnimations];
 }

This is a pretty common topic and has many answers out there.

Situation: I have a full screen (minus toolbar) UITextView with a UIToolbar at the bottom. when the UITextView gets first responder I want to have the toolbar slide up with the keyboard and add a "done" button which will dismiss the keyboard.

So far: I have this completely working, based on this example. Except for the fact that when I put [textView becomeFirstResponder]; in my viewDidLoad then the toolbar doesn't animate. Even though keyboardWillShow is called. Does anyone have any idea?

Code: Just so you don't have to check out the example code this is what is happening:

In viewDidLoad:

- (void)viewDidLoad {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
   [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
   [nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
   [textView becomeFirstResponder];
       [super viewDidLoad];
}

In keyboardWillShow:

- (void)keyboardWillShow:(NSNotification *)notification {
NSLog(@"keyboard will show");
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                                            target:self 
                                                                            action:@selector(keyboardDone)];
    NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:[toolbar items]];
    [toolbarItems addObject:doneButton];
    [toolbar setItems:toolbarItems];

    CGRect frame = self.view.frame;
    frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
    self.view.frame = frame;
     [UIView commitAnimations];
 }

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

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

发布评论

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

评论(2

月野兔 2024-09-15 09:00:06

尝试将 -becomeFirstResponder 调用移至 -viewWillAppear:animated:-viewDidAppear:animated:。我认为 -viewDidLoad 通常在视图实际添加到视图层次结构之前调用。

Try moving the -becomeFirstResponder call to -viewWillAppear:animated: or -viewDidAppear:animated:. I think -viewDidLoad is usually called just before the view's actually added to the view hierarchy.

羅雙樹 2024-09-15 09:00:06

将其添加到您的代码中

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{

    UIToolbar* keyboardDoneButtonView   = [[UIToolbar alloc] init];
    keyboardDoneButtonView.barStyle     = UIBarStyleBlack;
    keyboardDoneButtonView.translucent  = YES;
    keyboardDoneButtonView.tintColor    = nil;
    [keyboardDoneButtonView sizeToFit];

    UIBarButtonItem* doneButton    = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered  target:self action:@selector(doneBtnTapped:)];

    // I put the spacers in to push the doneButton to the right side of the picker view
    UIBarButtonItem *spacer1    = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                                target:nil action:nil];

    // I put the spacers in to push the doneButton to the right side of the picker view
    UIBarButtonItem *spacer    = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                               target:nil action:nil];

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:spacer, spacer1, doneButton, nil]];

    // Plug the keyboardDoneButtonView into the text field...
    textView.inputAccessoryView = keyboardDoneButtonView;

    return YES;
}

- (void)doneBtnTapped:(id)sender {
     [yourTextView resignFirstResponder];
}

并且一切都完成了......

Add this in your code

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{

    UIToolbar* keyboardDoneButtonView   = [[UIToolbar alloc] init];
    keyboardDoneButtonView.barStyle     = UIBarStyleBlack;
    keyboardDoneButtonView.translucent  = YES;
    keyboardDoneButtonView.tintColor    = nil;
    [keyboardDoneButtonView sizeToFit];

    UIBarButtonItem* doneButton    = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered  target:self action:@selector(doneBtnTapped:)];

    // I put the spacers in to push the doneButton to the right side of the picker view
    UIBarButtonItem *spacer1    = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                                target:nil action:nil];

    // I put the spacers in to push the doneButton to the right side of the picker view
    UIBarButtonItem *spacer    = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                               target:nil action:nil];

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:spacer, spacer1, doneButton, nil]];

    // Plug the keyboardDoneButtonView into the text field...
    textView.inputAccessoryView = keyboardDoneButtonView;

    return YES;
}

- (void)doneBtnTapped:(id)sender {
     [yourTextView resignFirstResponder];
}

And its all done...

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