이것은 꽤 일반적인 주제이며 많은 해답이 있습니다.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];
}
당신은 놀라운!이 추가 고맙습니다. 그것은 작동하지 않습니다 -viewWillAppear : animated :하지만 -viewDidAppear : animated와 같습니다. 로드 된 순서를 알지 못했기 때문에 오늘 새로운 것을 가르쳐 주신 것에 감사드립니다. – RyanJM