简体   繁体   English

仅当 swift 中出现键盘时如何使 scrollView 滚动

[英]How to make scrollView scrolling only if keyboard appears in swift

I am using scrolview for view with height 1000, initially i don't want my scrolView to scroll.我正在使用 scrollview 来查看高度为 1000 的视图,最初我不希望我的 scrollView 滚动。 if i tap on any textField then i want my scrolview to scroll and if i return keyboard then i don't want my scrollview to scroll.如果我点击任何文本字段,那么我希望我的滚动视图滚动,如果我返回键盘,那么我不希望滚动视图滚动。

Here i am able to textfield up when when keyboard appears but i am unable to return textfield to its orginal position when i return keyboard and when i return keyboard i dont want my view to scroll,在这里,当键盘出现时,我可以向上显示文本字段,但是当我返回键盘时,我无法将文本字段返回到其原始 position 并且当我返回键盘时,我不希望我的视图滚动,

Please help me in the code.请在代码中帮助我。

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var scrolView: UIScrollView!
@IBOutlet weak var upTFLD: UITextField!
var activeTextField = UITextField()

@IBOutlet weak var downTFLD: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    upTFLD.delegate = self
    downTFLD.delegate = self

    NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardAppear(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardDisappear(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
}
@objc func onKeyboardAppear(_ notification: NSNotification) {

    let info = notification.userInfo!
    let rect: CGRect = info[UIResponder.keyboardFrameBeginUserInfoKey] as! CGRect
    let kbSize = rect.size
    let insets = UIEdgeInsets(top: 0, left: 0, bottom: kbSize.height+20, right: 0)
    self.scrolView.contentInset = insets
    self.scrolView.scrollIndicatorInsets = insets
    var visibleRect: CGRect = self.scrolView.convert(self.scrolView.bounds, to: self.view)
    visibleRect.size.height -= rect.size.height;
    let inputRect: CGRect = self.activeTextField.convert(self.activeTextField.bounds, to: self.scrolView)
    if (visibleRect.contains(inputRect)) {
        self.scrolView.scrollRectToVisible(inputRect, animated: true)
    }
}

@objc func onKeyboardDisappear(_ notification: NSNotification) {

    self.scrolView.contentInset = UIEdgeInsets.zero
    self.scrolView.scrollIndicatorInsets = UIEdgeInsets.zero
}

public func textFieldDidBeginEditing(_ textField: UITextField) {

    activeTextField = textField
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    activeTextField.resignFirstResponder()
    return true
}
}

initially i dont want scrolling, and if i return keyboard i need textfield come to its original position and no scrolling again.最初我不想滚动,如果我返回键盘,我需要文本字段来到它原来的 position 并且不再滚动。

Only keyboard appears then only i need scrolling.只有键盘出现然后只有我需要滚动。 please help me in the code.请在代码中帮助我。

In viewWillAppear在视图中会出现

 yourScrollview.isScrollEnabled = false

After Keyboard appears make it true键盘出现后使它成为true

 yourScrollview.isScrollEnabled = true

Alternatively you can use IQKeyboard manager to take care of textfields.Checkout: IQKeyboardManager或者,您可以使用 IQKeyboard 管理器来处理 textfields.Checkout: IQKeyboardManager

You just need to set the contentOffset of your ScrollView right after the keyboard is hidden.您只需要在键盘隐藏后立即设置 ScrollView 的 contentOffset。

Create a variable to store offsetBeforeShowKeyboard创建一个变量来存储offsetBeforeShowKeyboard

var offsetBeforeShowKeyboard: CGFloat?

When view is initially loaded:最初加载视图时:

self.scrollView.isScrollEnabled = false

When select any TextField:当 select 任何 TextField:

public func textFieldDidBeginEditing(_ textField: UITextField) {
    self.scrollView.isScrollEnabled = true
    if (self.offsetBeforeShowKeyboard == nil) {
        self.offsetBeforeShowKeyboard = self.scrollView.contentOffset
    }
 }

When keyboard is hidden键盘隐藏时

 @objc func onKeyboardDisappear(_ notification: NSNotification) {
    self.scrollView.isScrollEnabled = false
    if let offset = self.offsetBeforeShowKeyboard {
        self.scrolView.setContentOffset(offset, animated: true)    
    }
    self.offsetBeforeShowKeyboard = nil
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM