2

I am using scrolview for view with height 1000, initially i don't want my scrolView to scroll. 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,

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.

Only keyboard appears then only i need scrolling. please help me in the code.

2 Answers 2

1

You just need to set the contentOffset of your ScrollView right after the keyboard is hidden.

Create a variable to store offsetBeforeShowKeyboard

var offsetBeforeShowKeyboard: CGFloat?

When view is initially loaded:

self.scrollView.isScrollEnabled = false

When select any 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
 }
Sign up to request clarification or add additional context in comments.

2 Comments

One doubt, here i gave 1000 fixed height, but how i can change its height according to iphone screensize.. which constraints need to give?
I think better implementation is making constraints between you scrollView and the view of your controller. If you still wanna fix height but depends on iphone screen sizes then you need to check device model and update your height. For iphone model checking, you can use some third party, such as: github.com/devicekit/DeviceKit
1

In viewWillAppear

 yourScrollview.isScrollEnabled = false

After Keyboard appears make it true

 yourScrollview.isScrollEnabled = true

Alternatively you can use IQKeyboard manager to take care of textfields.Checkout: IQKeyboardManager

Comments

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.