2010-07-01 69 views
1

这是一个相当普遍的话题,并有很多答案。与第一响应者的键盘顶部的工具栏

情况:我有一个完整的屏幕(减去工具栏)UITextView UIToolbar在底部。当UITextView获得第一响应者时,我想让工具栏向上滑动键盘并添加一个“完成”按钮,它将关闭键盘。

到目前为止:我有这个完全工作,基于this example。除了当我将[textView becomeFirstResponder];放入我的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]; 
} 

回答

3

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

+0

你是了不起的!收藏此谢谢。它在-viewWillAppear中无效:动画:但它与-viewDidAppear一起使用:动画。我不知道那些加载的顺序,所以谢谢你今天教我新东西。 – RyanJM 2010-07-01 22:13:52

0

在你的代码

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

和所有做...