This question already has answers here:
How do I scroll the UIScrollView when the keyboard appears?
                                
                                    (22个答案)
                                
                        
                                2年前关闭。
            
                    
有一个分段的控件,每个控件都有不同的高度。每个字段中都有一些文本字段。当我在文本字段上点击然后在另一段上点击时,在显示键盘时无法滚动到视图底部。
我想用这些无效的代码修复此问题。

if(self.isKeyBoardShowing) {
    CGRect borderLabelRect = containerView.frame;
    self.scrollView.contentSize = CGSizeMake(borderLabelRect.size.width , borderLabelRect.size.height + 325);
    [self.view layoutIfNeeded];
    [self.view layoutSubviews];
}

最佳答案

在viewDidLoad中添加观察者

NotificationCenter.default.addObserver(self, selector: #selector(onShowKeyboard), name: NSNotification.Name.UIKeyboardWillShow, object: nil)


并将选择器实现为->

 @objc func onShowKeyboard(_ notification:Notification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
    }
}

关于ios - 当iOS中显示键盘时,无法滚动至 View 底部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53902107/

10-11 15:06