gpt4 book ai didi

ios - UIScrollView 在键盘关闭时停止滚动

转载 作者:行者123 更新时间:2023-11-28 21:15:17 25 4
gpt4 key购买 nike

我目前正在使用 Swift 3 为 iPhone 开发一个应用程序,但是我遇到了 ScrollView 的问题。在选择文本字段并显示键盘之前, ScrollView 正常工作(即:我可以上下滚动),但是在关闭键盘后, ScrollView 不再允许我滚动,这是我的问题。注意:如果我再次选择一个文本字段并使键盘出现,它可以正常工作并在再次关闭后停止工作。

我检查了 ScrollView 的 isScrollEnabled 属性,它似乎已启用。不幸的是,我仍然不太熟悉 ScrollView 的所有细节,似乎无法弄清楚它停止工作的原因。

任何关于我可以在哪里寻找的帮助或指示将不胜感激。

编辑:有相当多的代码,但这里是与 ScrollView 和键盘相关的缩小部分:

class ScrollViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {
//Scroll view
@IBOutlet weak var scrollView: UIScrollView!
//UIView inside the scroll view
@IBOutlet weak var contentView: UIView!
//Save button on the top right corner
@IBOutlet weak var saveButton: UIBarButtonItem!
//Text field being editted
var activeTextField:UITextField?

fileprivate var contentInset:CGFloat?
fileprivate var indicatorInset:CGFloat?

override func viewDidLoad() {
contentInset = scrollView.contentInset.bottom
indicatorInset = scrollView.scrollIndicatorInsets.bottom

NotificationCenter.default.addObserver(self,
selector: #selector(ScrollViewController.keyboardWillShow(_:)),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(ScrollViewController(_:)),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
}

func adjustInsetForKeyboardShow(_ show: Bool, notification: Notification) {
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let adjustmentHeight = (keyboardFrame.height + 20) * (show ? 1 : -1)

scrollView.contentInset.bottom = (contentInset! + adjustmentHeight)
scrollView.scrollIndicatorInsets.bottom = (indicatorInset! + adjustmentHeight)
}

func keyboardWillShow(_ notification: Notification) {
adjustInsetForKeyboardShow(true, notification: notification)
}

func keyboardWillHide(_ notification: Notification) {
adjustInsetForKeyboardShow(false, notification: notification)
}

//Tap gesture to dismiss the keyboard
@IBAction func hideKeyboard(_ sender: AnyObject) {
self.view.endEditing(false)
}

deinit {
NotificationCenter.default.removeObserver(self);
}
}

最佳答案

我已经创建了 UIViewController 的扩展和 scrollView 的创建方法。只需要调用 viewWillAppear() 和 viewDidDisappear()

extension UIViewController  {

func registerForKeyboradDidShowWithBlock (scrollview:UIScrollView ,block: ((CGSize?) -> Void)? = nil ){
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardDidShow, object: nil, queue: nil) { (notification) in
if let userInfo = (notification as NSNotification).userInfo {
if let keyboarRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.findFirstResponder() != nil {
let keyboarRectNew = self.view .convert(keyboarRect, to: self.view)
let scrollViewSpace = scrollview.frame.origin.y + scrollview.contentOffset.y
let textFieldRect:CGRect = self.view.findFirstResponder()!.convert(self.view.findFirstResponder()!.bounds, to: self.view)
let textFieldSpace = textFieldRect.origin.y + textFieldRect.size.height
let remainingSpace = self.view.frame.size.height - keyboarRectNew.size.height

if scrollViewSpace + textFieldSpace > remainingSpace {
let gap = scrollViewSpace + textFieldSpace - remainingSpace
scrollview .setContentOffset(CGPoint(x: scrollview.contentOffset.x, y: gap), animated: true)
}
}
}
}
block?(CGSize.zero)

}
}

func registerForKeyboardWillHideNotificationWithBlock ( scrollview:UIScrollView ,block: ((Void) -> Void)? = nil) {
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: nil, using: { (notification) -> Void in
scrollview.scrollRectToVisible(CGRect(x: 0, y: 0, width: 0, height: 0), animated: true)
scrollview.contentOffset = CGPoint(x: 0, y: 0)
scrollview.contentInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
scrollview.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);

block?()
})
}

func deregisterKeyboardShowAndHideNotification (){

NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)

self.view.findFirstResponder()?.resignFirstResponder()
}
}

我已经创建了 UIView 的扩展以在其中创建查找第一响应者方法。

extension UIView {
func findFirstResponder() -> UIView? {
if self.isFirstResponder {
return self
}
for subview: UIView in self.subviews {
let firstResponder = subview.findFirstResponder()
if nil != firstResponder {
return firstResponder
}
}
return nil
}
}

关于ios - UIScrollView 在键盘关闭时停止滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com