IPhones-让键盘滑出视图

时间:2012-04-27 04:13:00

标签: iphone objective-c keyboard

我们打开了一个显示键盘的视图,但是当单击后退按钮时,视图会从右侧滑出,而键盘只有在视图消失时才会滑动。 如果我们在viewwilldisappear调用resignFirstResponder,则视图会在键盘同时向下滑动时向右滑动。 是否可以让键盘滑出视图?

4 个答案:

答案 0 :(得分:1)

没有标准的方法可以做你想要的,但是......
基本上,键盘只是一个视图,在所有其他窗口的顶部显示在它自己的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视图。这样你的键盘就会随着视图一起滑动。

答案 1 :(得分:1)

我已经测试了这个并且它在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来检测键盘何时隐藏,或者只是手动使用上述代码。

答案 2 :(得分:0)

尝试在resignFirstresponder方法中坚持使用viewDidDisappear

答案 3 :(得分:0)

          **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;