简体   繁体   English

具有firstresponder的键盘顶部的工具栏

[英]toolbar on top of the keyboard with firstresponder

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. 情况 :我有一个全屏(减工具栏)UITextView,底部有一个UIToolbar。 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. 当UITextView获得第一响应者时,我希望工具栏随键盘向上滑动,并添加一个“完成”按钮以关闭键盘。

So far : I have this completely working, based on this example . 到目前为止 :根据此示例 ,我已经完全完成了这一工作。 Except for the fact that when I put [textView becomeFirstResponder]; 除了我放[textView becomeFirstResponder];的事实之外[textView becomeFirstResponder]; in my viewDidLoad then the toolbar doesn't animate. 在我的viewDidLoad则工具栏不设置动画。 Even though keyboardWillShow is called. 即使keyboardWillShow被调用。 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: 在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];
 }

Try moving the -becomeFirstResponder call to -viewWillAppear:animated: or -viewDidAppear:animated: . 尝试将-becomeFirstResponder调用移至-viewWillAppear:animated:-viewDidAppear:animated: I think -viewDidLoad is usually called just before the view's actually added to the view hierarchy. 我认为-viewDidLoad通常在将视图实际添加到视图层次结构之前调用。

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... 一切都完成了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM