2010-07-01 7 views
1

이것은 꽤 일반적인 주제이며 많은 해답이 있습니다.firstresponder가있는 키보드 상단에 도구 모음

상황 : 하단에 UIToolbar가있는 전체 화면 (툴바 제외) UITextView가 있습니다. UITextView가 첫 번째 응답자가되면 툴바를 키보드로 밀어 넣고 "완료"버튼을 추가하여 키보드를 닫을 수 있습니다.

지금까지 :이 작업은 this example을 기반으로하고 있습니다. 사실 [textView becomeFirstResponder];을 내 viewDidLoad에 넣을 때를 제외하고 툴바는 움직이지 않습니다. keyboardWillShow이 호출 되더라도. 누구 아이디어가 있습니까?

코드 :

의 viewDidLoad에서

: keyboardWillShow에서

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

:

- (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
그냥 그래서 당신이없는이 무슨 일이 일어나고있는 예제 코드를 체크 아웃

전화를 -viewWillAppear:animated: 또는 -viewDidAppear:animated:으로 이동해보십시오. 나는 실제로보기 계층 구조에 실제로 추가되기 바로 전에 -viewDidLoad이 호출된다고 생각합니다.

+0

당신은 놀라운!이 추가 고맙습니다. 그것은 작동하지 않습니다 -viewWillAppear : animated :하지만 -viewDidAppear : animated와 같습니다. 로드 된 순서를 알지 못했기 때문에 오늘 새로운 것을 가르쳐 주신 것에 감사드립니다. – RyanJM

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

을 그리고 그 모든 일 ...