首页 文章

UIKeyboardDidShowNotification多次调用,有时键盘尺寸不正确

提问于
浏览
13

我试图在键盘出现/更改时在键盘上方移动UITextView . 让's say I have the English keyboard displaying and then switch directly to the Chinese keyboard (which is taller than the standard English keyboard). In this scenario my text view always appears too high (to the naked eye, it looks like the text view is incorrectly offset by the size of the Chinese keyboard 2448143 . I'发布一张图片但缺乏声誉:)) . 当我的应用程序收到UIKeyboardDidShowNotification(使用UIKeyboardFrameEndUserInfoKey获取高度)时,我正在调整我的文本视图位置,并且经过一些调查 UIKeyboardDidShowNotification is called multiple times, oftentimes with the incorrect keyboard dimensions (我在响应UIKeyboardDidShowNotification的方法中使用了NSLogged 'self',它实际上始终是相同的View Controller宾语 .

但是,即使多次触发此通知,我仍然不明白为什么某些通知的键盘高度会有所不同 . 其中一个通知始终具有正确的高度,但是当它不是最后一个通知时,文本视图最终会出现在错误的位置 . 任何有关如何进一步排除故障的见解将非常感激!

编辑:我测试的越多,特别是中文键盘的问题就越多 . 每当我将键盘从英文切换到中文时,我得到三个UIKeyboardWillShowNotifications:

2014-12-24 22:49:29.385 Example[1055:421943] info dictionary: {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 252}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 460}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 442}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 316}, {320, 252}}";
}
2014-12-24 22:49:29.408 Example[1055:421943] info dictionary: {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 442}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 316}, {320, 252}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
}
2014-12-24 22:49:29.420 Example[1055:421943] info dictionary: {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 288}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 442}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 424}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 316}, {320, 252}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 280}, {320, 288}}";
}

第一个具有正确的结束高度:252 . 然而,接下来的两个在216和288处是不正确的 . 这可靠地发生 .

以下是一些片段,用于演示我如何管理订阅通知:

-(void)viewWillAppear:(BOOL)animated {

       [super viewWillAppear:animated];


       [self registerForKeyboardNotifications];


}

-(void)viewWillDisappear:(BOOL)animated {
       [super viewWillDisappear:animated];

       [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                                object:nil];
       [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardDidShowNotification
                                                object:nil];

}

- (void)registerForKeyboardNotifications {



      [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardWillHideNotification object:nil];

      [[NSNotificationCenter defaultCenter] addObserver:self      
                                         selector:@selector(keyboardDidShow:)                                       
                                             name:UIKeyboardDidShowNotification object:nil];
}

4 回答

  • 5

    如果您在模拟器上使用Cmd K快捷键来显示/隐藏键盘,则可能会多次调用它,因为它不会将文本字段作为第一响应者重新调用 .

    如果您改为使用键盘的 Return 按钮,那么它将做正确的事情并将其重新签名 .

  • 4

    主要原因是您的通知被多次调用 . 例如,在swift中,如果您在viewWillAppear方法中使用了NSNotificationCenter.defaultCenter() . addObserver(xx“keyboardWillShow”xx),并且如果您在视图之间来回移动,那么它将导致具有相同的多个观察者“ keyboardWillShow” . 相反,您应该考虑将addObserver调用移动到“viewDidLoad()”方法 . 或者,您可以在视图显示/消失时注册/取消注册观察者 .

  • 0

    就我而言, I register for my keyboard notifications in ViewWillAppear and unregister in ViewWillDisappear 也是如此 . 但是,它会导致多次触发UIKeyboardDidShowNotification处理程序 . 看起来像取消注册方法不起作用,所以,每次出现视图时,Observer for UIKeyboardDidShowNotification加1.然后,你触摸UITextField,多个Observer会被通知,处理程序会一次又一次被触发 .

    因此,您必须在ViewDidLoad中注册键盘通知,而不要取消注册 . 就像page中提到的Apple一样,

    //在视图控制器设置代码中的某处调用此方法 .

    我认为'setup'意味着ViewDidLoad.And在处理键盘通知代码列表中,没有ViewWillDisappear .

    这是我的键盘通知处理程序,它工作 .

    func keyboardWillShow(notification: NSNotification) {
        let userInfo = notification.userInfo!
        let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height
        debugPrint("Before",NotifyView.frame)
        NotifyView.frame.offsetInPlace(dx: 0, dy: -keyboardHeight)
        debugPrint("After",NotifyView.frame)
    }
    func keyboardWillHide(notification: NSNotification) {//Do Nothing
    }
    
  • 0
    - (void)keyboardDidAppear:(NSNotification *)note {
        CGSize keyboardSize = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
        textView.contentInset = contentInsets;
        textView.scrollIndicatorInsets = contentInsets;
    }
    
    - (void)keyboardWillBeHidden:(NSNotification *)note {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        textView.contentInset = contentInsets;
        textView.scrollIndicatorInsets = contentInsets;
    }
    

    解决方案来自Apple的官方解决方案here,就像曹说的那样 .

相关问题