0

I have a strange problem. The keyboard works when I have the app connected to Xcode. If app turn off and on again, the keyboard with UITextView and UITextField no longer works... but UISearchField works.

I use this methods:

- (void)addKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(questionCellTextViewBeginEditing:) name:@"QuestionCellTextViewBeginEditingNotification" object:nil];
}

For example keyboardWillShow in Objective-C look like:

- (void)keyboardWillShow:(NSNotification *)notification {
    [_tableView setContentBottomInset:[Tools keyboardHeightFromNotification:notification]];
    
    [self.view layoutIfNeeded];
    
    [UIView animateWithDuration:0.3f animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [_tableView scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }];
}

And I use Swift version in a few places in app:

@objc func keyboardWillShow(sender: Notification) {
        keyboardHeight = (sender.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect).size.height
        
        if #available(iOS 11.0, *) {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            
            if (appDelegate.window?.safeAreaInsets.top)! > CGFloat(0.0) || appDelegate.window?.safeAreaInsets != .zero {
                keyboardHeight -= (appDelegate.window?.safeAreaInsets.bottom)!
            }
        }
        
        let y = self.tableView.contentOffset.y + keyboardHeight
        self.tableView.contentOffset = CGPoint.init(x: 0, y: y)
        
        self.textViewContainerBottomConstraint?.constant = keyboardHeight
        
        self.view?.layoutIfNeeded()
        UIView.animate(withDuration: 0.3) {
            self.view?.layoutIfNeeded()
        }
    }

I am wondering about this log printed in console when I terminated app:

[27682:19912067] [Snapshotting] Snapshotting a view (0x7fa4e304d400, UIKeyboardImpl) that is not in a visible window requires afterScreenUpdates:YES.

Do you know where is problem? I have no idea how to test an application without being debugged in Xcode.

3
  • Does it work correctly on a device? The simulator will attach your keyboard which can have strange side effects but you can use a device to test or turn that off in the simulator and also see if that makes a difference. In the simulator, switch off I/O > Keyboard > Connect hardware keyboard and see if it works. Commented Sep 28, 2020 at 5:50
  • @skaak I tested on a physical device too. Commented Sep 28, 2020 at 8:15
  • I do something vaguely similar - also with constraints to change layout based on keyboard but I use keyboardDidShow/Hide ... maybe give that a try. Not entirely sure what you mean with the keyboard no longer works? Commented Sep 28, 2020 at 8:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.