キーボードが表示された状態でビューが開いていますが、戻るボタンをクリックするとビューが右からスライドして表示されますが、キーボードはビューが消えたときにのみスライドします。ビューで rejectFirstResponder を呼び出すと、ビューは右にスライドし、キーボードは同時に下にスライドします。ビューでキーボードをスライドさせることは可能ですか?
4 に答える
あなたが望むことを行うための標準的な方法はありませんが...
基本的に、キーボードは単なるビューであり、他のすべてのウィンドウの上にある独自の UIWindow に表示されます。
したがって、理論的には、キーボード ビューを見つけて、目的の方向に移動する必要があります。プロパティを使用する必要がtransform
あり、を台無しにしないでくださいframe
。
Class keyboardClass = NSClassFromString(@"UIPeripheralHostView");
for ( UIWindow *window in [[UIApplication sharedApplication] windows] ) {
for ( UIView *subview in window.subviews ) {
if ( [subview isKindOfClass:keyboardClass] ) {
// that's keyboard
}
}
}
編集:
UINavigationController について話している場合、それがプッシュ/ポップ中のデフォルトのスライド アニメーションである場合は、テキスト ビューでresignFirstResponder
インviewDidDisappear
とbecomeFirstResponder
インを呼び出すだけです。viewWillAppear
そうすれば、キーボードがビューに沿ってスライドします。
これをテストしたところ、iOS 5.1 で動作しますが、これは推奨される動作ではないと思います。
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
if ([[keyboardWindow description] hasPrefix:@"<UITextEffectsWindow"]) {
NSLog(@"%@", [keyboardWindow description]);
[UIWindow beginAnimations:@"fadeKeyboard" context:nil];
keyboardWindow.frame = CGRectMake(keyboardWindow.frame.origin.x + keyboardWindow.frame.size.width, keyboardWindow.frame.origin.y, keyboardWindow.frame.size.width, keyboardWindow.frame.size.height);
[UIWindow commitAnimations];
}
通知 UIKeyboardWillHideNotification を使用して、キーボードが非表示になるタイミングを検出するか、上記のコードを手動で使用することもできます。
代わりにメソッドに固執resignFirstresponder
してみてください。viewDidDisappear
**Set notificatins and use these methods.....Hope it solve problem:
First of all set your whole view in scrollView**
-(void)keyboardDidHide:(NSNotification *)notif
{
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
scrollView.contentSize=CGSizeMake(320,scrollOriginalFrame.size.height);
}];
keyboardVisible=NO;
}
-(void)keyboardDidShow:(NSNotification *)notif
{
scrollView.contentSize=CGSizeMake(self.view.frame.size.width, scrollOriginalFrame.size.height+235);
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
[scrollView setContentOffset:CGPointMake(0,162) animated:YES];
}];
keyboardVisible=YES;
}
**In viewDidLoad() add this**
//keyboard
scrollOriginalFrame=self.view.frame;
scrollOriginalFrame.size.height-=103;
scrollView.contentSize=scrollOriginalFrame.size;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
keyboardVisible=NO;