Scrollview doesn't move up when keyboard appears several times
friend please try this piece of code. Hope it will work for you too. I am using this and it works well.
override func viewDidLoad(){
NotificationCenter.default.addObserver(self,selector: #selector(self.keyboardDidShow(notification:)),
name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self,selector: #selector(self.keyboardDidHide(notification:)),
name: UIResponder.keyboardDidHideNotification, object: nil)
}
//MARK: Methods to manage keybaord
@objc func keyboardDidShow(notification: NSNotification) {
var info = notification.userInfo
let keyBoardSize = info![UIKeyboardFrameEndUserInfoKey] as! CGRect
scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
}
@objc func keyboardDidHide(notification: NSNotification) {
scrollView.contentInset = UIEdgeInsets.zero
scrollView.scrollIndicatorInsets = UIEdgeInsets.zero
}
I have written this code for swift 4.0.
How to make scrollView scrolling only if keyboard appears in swift
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
}
Handling keyboard events when view controller has a scrollview inside
The problem in your didReceiveKeyboardNotification
method. You are using the same method for handling show/hide keyboard, and in both cases, you make scrollView.contentInset.bottom = keyboardOverlap
so after keyboard will hide your inset will be set to 0. This is more correct way:
if notification.name == UIResponder.keyboardWillHideNotification {
let inset = tabBarController?.tabBar.frame.size.height as! CGFloat //Tabbar height
scrollView.contentInset.bottom = inset
scrollView.verticalScrollIndicatorInsets.bottom = inset
}
if notification.name == UIResponder.keyboardWillShowNotification {
scrollView.contentInset.bottom = keyboardOverlap
scrollView.verticalScrollIndicatorInsets.bottom = keyboardOverlap
}
scrollView problem with UITextView when keyboard hide/show notification
The reason of this issue is explained here, so if you want to use UITextView
inside a UIScrollView
then uncheck the Scrolling Enabled
from right menu inspector or set it False
from the code.
How to make scrollview up to keyboard height according to iPhone sizes in swift
Remove background colour of scrollview in your storyboard .
You can follow my approach for more clarifications .
moving-keyboard-when-editing-multiple-textfields-with-constraints-swift
UIScrollView scroll up on Keyboard
Replace keyboardWasShown function with the below function :
func keyboardWasShown(notification: NSNotification)
{
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.convert(keyboardFrame, from: nil)
self.mainScroll.contentOffset = CGPoint(x: 0, y: keyboardFrame.size.height - Any number that fits your need.)
}
In keyBoardWillHide :
self.mainScroll.contentOffset = CGPoint(x: 0, y: 0)
Hope it will help.
Happy Coding!
Keyboard covers UITextView
For UITextView it doesn't work, so i do this with contentOffset.
Swift 5
extension SummaryViewController {
private func registerKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIControl.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIControl.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(_ notification: Notification) {
guard let keyboardFrameValue = notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue else { return }
let keyboardFrame = view.convert(keyboardFrameValue.cgRectValue, from: nil)
scrollView.contentOffset = CGPoint(x: 0, y: keyboardFrame.size.height)
}
@objc func keyboardWillHide(_ notification: Notification) {
scrollView.contentOffset = .zero
}
}
Related Topics
MySQL and Swift - Upload Image and File || Would It Be Better to Use Alamofire
Core Data Does Not React on Changed Predicate
The Paper Folding/Unfolding Effect in Twitter for iPad
Uicollectionview with a Sticky Header
How to Add Private Key to the Distribution Certificate
Uinavigationcontroller Without Navigation Bar
Notify Watchkit App of an Update Without the Watch App Requesting It
Google Analytics iOS Campaign Tracking and Url Builder
Uilabel Text Not Being Updated
How to Adjust the Height of a Textview to His Content in Swift
Swift 3.0, Alamofire 4.0 Extra Argument 'Method' in Call
Swift Array Append Overwriting Other Array Values
How Do iOS Push Notifications Work
Frosted Glass (iOS 7 Blur) Effect
Using iOS Gamekit's "Bluetooth Bonjour" with Other Platforms
How to Distribute iOS Applications Outside App Store