FirStresponderを備えたキーボードの上にあるツールバー
-
02-10-2019 - |
質問
これはかなり一般的なトピックであり、そこに多くの答えがあります。
状況: :下部にuitoolbarを備えたフルスクリーン(マイナスツールバー)uitextviewがあります。 uitextViewが最初のレスポンダーを取得したら、キーボードでツールバーをスライドさせ、キーボードを却下する「完了」ボタンを追加したいと思います。
ここのところ: :私はこれに基づいて完全に機能しています この例. 。私が置いたときの事実を除いて [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];
}
解決
動いてみてください -becomeFirstResponder
へ電話する -viewWillAppear:animated:
また -viewDidAppear:animated:
. 。おもう -viewDidLoad
通常、ビューの階層に実際に追加される直前に呼ばれます。
他のヒント
これをコードに追加します
- (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];
}
そしてそれはすべて完了です...
所属していません StackOverflow