我正在尝试在显示键盘时滚动到UIScrollView的底部内容,但方法setContentOffset似乎不起作用。我在Interface Builder上使用AutoLayout。也许这是导致问题的原因?有什么想法吗?
当我显示键盘时,如何滚动到UIScrollView的底部。
P.S。:它会显示textField就好了,我需要的是在键盘显示时显示UIScrollView的最底部
谢谢!!
以下是我当前的代码
func textFieldDidBeginEditing(textField: UITextField) {
var bottomOffset: CGPoint = CGPoint(x: 0, y: mainScrollView.contentSize.height - mainScrollView.bounds.size.height);
mainScrollView.setContentOffset(bottomOffset, animated: true);
}
func keyboardDidShow(notification: NSNotification) {
var userInfoDictionary: NSDictionary = notification.userInfo!;
var keyboardSize: CGSize = userInfoDictionary.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size;
var contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0);
mainScrollView.contentInset = contentInsets;
mainScrollView.scrollIndicatorInsets = contentInsets;
}
func keyboardWillHide(notification: NSNotification) {
var contentInsets = UIEdgeInsetsZero;
mainScrollView.contentInset = contentInsets;
mainScrollView.scrollIndicatorInsets = contentInsets;
}
答案 0 :(得分:4)
在keyboardDidShow
方法中添加类似的内容:
var offset = scrollView.contentOffset
offset.y = scrollView.contentSize.height + scrollView.contentInset.bottom - scrollView.bounds.size.height
scrollView.setContentOffset(offset, animated: true)
答案 1 :(得分:1)
我遇到与原始海报完全相同的问题,无法得到其他答案。我认为它与iOS 11的自动滚动文本字段的功能有关,虽然这是未经证实的。
我确实找到了其他几种处理方法。
UIView.animate(withDuration: 0.3, animations: {
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
}, completion: { (done) in
var offset = self.scrollView.contentOffset
offset.y = keyboardSize.height
self.scrollView.setContentOffset(offset, animated: true)
})
(注意:如果底部有标签栏或工具栏等现有插件,这可能不太好)
additionalSafeAreaInsets.bottom = keyboardSize.height
var offset = self.scrollView.contentOffset
offset.y = keyboardSize.height
self.scrollView.setContentOffset(offset, animated: true)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
DispatchQueue.main.async {
var offset = self.scrollView.contentOffset
offset.y = keyboardSize.height
self.scrollView.setContentOffset(offset, animated: true)
}
答案 2 :(得分:1)
让我滚动到滚动视图的底部意味着将偏移量设置为等于总内容高度减去实际的滚动视图高度
例如,要在键盘出现时滚动到UIScrollView的底部,我使用以下代码:
目标-C
- (void)onKeyboardDidShow:(NSNotification *)notification {
NSValue *value = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGFloat keyboardHeight = value.CGRectValue.size.height;
self.scrollViewBottomConstraint.constant = keyboardHeight;
CGPoint offset = self.scrollView.contentOffset;
CGFloat scrollViewHeight = self.scrollView.frame.size.height - keyboardHeight;
offset.y = self.scrollView.contentSize.height - scrollViewHeight;
[self.scrollView setContentOffset:offset animated:YES];
}
Swift 3
func onKeyboardDidShow(notification: NSNotification) {
if let value = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] {
let keyboardHeight = (value as AnyObject).cgRectValue.size.height
self.scrollViewBottomConstraint.constant = keyboardHeight
var offset = self.scrollView.contentOffset
let scrollViewHeight = self.scrollView.frame.size.height - keyboardHeight
offset.y = self.scrollView.contentSize.height - scrollViewHeight
self.scrollView.setContentOffset(offset, animated: true)
}
}
底部约束在出现时更改为键盘高度,并在消失时返回0。
有趣的是,滚动视图的实际高度不仅仅是self.scrollView.frame.size.height
(至少在键盘出现时)。我不得不减去键盘大小以获得真正的价值。