Can't move keyboard view iOS9 swift

和自甴很熟 提交于 2019-12-01 20:52:17

问题


I need to implement a dismissive keyboard (swiping down to dismiss) like the one in the stock messages app on iOS.

I have this code to get the keyboard view:

func keyboardWillShowWithNotification(notification:NSNotification) {
    let keyboardView = accessoryView.superview
}  

And I connected the UIPanGestureRecognizer of the tableView to detect when I need to start moving the keyboard down.

func handleTableViewPan(gr:UIPanGestureRecognizer) {

   let location = panGestureRecognizer.locationInView(self.view)
   let offset = ... //calculated correctly
   keyboardView.frame.origin.y = originalKeyboardFrame.origin.y + offset
}

The method worked fine with iOS 8 but with iOS 9 it seems like the keyboard is hold in place a little different so I can't move it. Maybe someone encountered the same problem and can help me. Thank you.


回答1:


In iOS 9 there is a new window for keyboard named UIRemoteKeyboardWindow, so when you are using accessoryView.superview you will get a wrong view.

To get correct view try to find it from window hierarchy directly: (objective-c code)

-(UIView*)getKeyboardInputView {
    if([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
        for(UIWindow* window in [[UIApplication sharedApplication] windows])
            if([window isKindOfClass:NSClassFromString(@"UIRemoteKeyboardWindow")])
                for(UIView* subView in window.subviews)
                    if([subView isKindOfClass:NSClassFromString(@"UIInputSetHostView")])
                        for(UIView* subsubView in subView.subviews)
                            if([subsubView isKindOfClass:NSClassFromString(@"UIInputSetHostView")])
                                return subsubView;
    } else {
        return accessoryView.superview;
    }
    return nil;
}

P.S. Taken from DAKeyboardControl https://github.com/danielamitay/DAKeyboardControl/pull/98



来源:https://stackoverflow.com/questions/32632207/cant-move-keyboard-view-ios9-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!