UIScrollView do not scroll down to its original position when keyboard disappear

UIScrollView do not scroll down to its original position when keyboard disappear

By : Bill Spitler
Date : November 19 2020, 03:01 PM
like below fixes the issue I have found the solution. Actually the concept behind scrollview was not clear to me. But now it is clear and change in one line only made the trick.
code :
- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary* info = [notification userInfo];
    kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect commentViewFrame = self.detailCommentView.frame;
    commentViewFrame.origin.y += kbSize.height;

    [UIView animateWithDuration:0.3 animations:^{
        [self.detailCommentView setFrame:commentViewFrame];
        self.scrollView.contentOffset = CGPointMake(0, 0);
    } completion:^(BOOL finished) {

Share : facebook icon twitter icon
How do I scroll the UIScrollView when the keyboard appears?

How do I scroll the UIScrollView when the keyboard appears?

By : Sabreen.S
Date : March 29 2020, 07:55 AM
hop of those help? The recommended way from Apple is to change the contentInset of the UIScrollView. It is a very elegant solution, because you do not have to mess with the contentSize. Following code is copied from the Keyboard Programming Guide, where the handling of this issue is explained. You should have a look into it.
code :
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
    [[NSNotificationCenter defaultCenter] addObserver:self
            name:UIKeyboardDidShowNotification object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self
             name:UIKeyboardWillHideNotification object:nil];

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [scrollView setContentOffset:scrollPoint animated:YES];

// Called when the UIKeyboardWillHideNotification is sent    
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
func registerForKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardAppear(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardDisappear(_:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)

// Don't forget to unregister when done
deinit {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardDidHide, object: nil)

@objc func onKeyboardAppear(_ notification: NSNotification) {
    let info = notification.userInfo!
    let rect: CGRect = info[UIKeyboardFrameBeginUserInfoKey] as! CGRect
    let kbSize = rect.size

    let insets = UIEdgeInsetsMake(0, 0, kbSize.height, 0)
    scrollView.contentInset = insets
    scrollView.scrollIndicatorInsets = insets

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    var aRect = self.view.frame;
    aRect.size.height -= kbSize.height;

    let activeField: UITextField? = [addressTextView, servicePathTextView, usernameTextView, passwordTextView].first { $0.isFirstResponder }
    if let activeField = activeField {
        if !aRect.contains(activeField.frame.origin) {
            let scrollPoint = CGPoint(x: 0, y: activeField.frame.origin.y-kbSize.height)
            scrollView.setContentOffset(scrollPoint, animated: true)

@objc func onKeyboardDisappear(_ notification: NSNotification) {
    scrollView.contentInset = UIEdgeInsets.zero
    scrollView.scrollIndicatorInsets = UIEdgeInsets.zero
UIScrollView scroll up on Keyboard

UIScrollView scroll up on Keyboard

By : Majeed Kocher Duhok
Date : March 29 2020, 07:55 AM
With these it helps Problem Statement : I have Nib file of UIView contains UIScrollview, In scrollview I have several TextFields and a TextView at the Bottom. What I want is to scroll upward when textfield or Textview starts editing. , Replace keyboardWasShown function with the below function :
code :
 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.)
self.mainScroll.contentOffset = CGPoint(x: 0, y: 0)
UIScrollView scroll to button when keyboard is visible

UIScrollView scroll to button when keyboard is visible

By : Bosko M.
Date : March 29 2020, 07:55 AM
this will help I got a pretty basic login form in my iOS application. The typical behavior I normally implement ist registering for the keyboard notifications and handling the scroll behavior of the UIScrollView in order to show the currently focused UITextField. However this time I need a slightly different behavior. , Did you try it the following way?
code :
func keyboardWillShow(notification: Notification) {
    self.scrollView.isScrollEnabled = true
    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, self.keyboardSize.height, 0.0)
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    var rect = self.view.frame
    rect.size.height -= self.keyboardSize.height
    if !rect.contains(self.loginButton.frame.origin) {
        self.scrollView.scrollRectToVisible(self.loginButton.frame, animated: true)
How to decide which UIScrollView to scroll when showing keyboard

How to decide which UIScrollView to scroll when showing keyboard

By : denisse og
Date : March 29 2020, 07:55 AM
I wish this helpful for you You should register for keyboard notification, I don't know if it would fix the problem but take a look here:Apple doc about keyboard.
I this way you will be able to scroll the scrollview with the keyboard on screen, but I guess that the webview will respond too, since it's already registered for keyboard notification, maybe you could also rescroll it listening to this kind of notification.
Why UIScrollView doesn't scroll when keyboard is shown?

Why UIScrollView doesn't scroll when keyboard is shown?

By : user5118305
Date : March 29 2020, 07:55 AM
it fixes the issue i had this problem when i had a scroll view in storyboard with AutoLayout , the problem is that UIScrollView with AutoLayout doesn't know its content size even if you set its content size programmatically , its content size is inferred from the constraints applied to its content views , and it can be quite messy if you have many subviews in the scroll view content , so i solved the problem by adding all subviews in the scrollview to a root view and add this root view as the content of my scroll view , and of course pin top , left , bottom , right , width and height of this root view to the scrollview , this way the scrollview knows its content view's size exactly , here is a technical note issued by apple about this problem https://developer.apple.com/library/ios/technotes/tn2154/_index.html
hope this helps !
Related Posts Related Posts :
  • Apartment number / Unit number returned from google places api
  • How to align UICollectionViewCells from left to right?
  • How can I add search option on UIPickerview in ios?
  • Scenekit - physicsWorld setup to prevent kinematic nodes to intersect
  • Carthage Build Failed
  • swift 3, ios 10 - push notification firebase is not received
  • App Xcode 8 to Xcode 9 - Prevent Auto Hide Status Bar
  • How to pop back to a TableViewController?
  • Is UserDefaults thread safe in Swift? How to check it?
  • UISwitch in a table selects multiple cells when switch state on
  • iOS App with no story board (Size classes and Device type limitations)
  • AudioKit issue, mic not sensitive enough
  • send indexpath to Firebase (like button)
  • UIColor saturation brightness values different from the regular saturation luminosity values of a color
  • JSON: Extracting Data Using Alamofire and Swift 3
  • invalid_grant on OAuth2 request when obtaining access_token from SSO in App
  • ABNewPersonViewController using contact framework
  • Design iOS button scalable
  • Subclassing NSLayoutConstraint constant based on screen height
  • Migrate Realm 2.x List of Object to Realm 3.x List of String (or other primary type)
  • Using BLE RSSI value for connect-by-proximity
  • Backgroundcolor of NSTextfield
  • Firebase modifying uid in every view
  • How to rotate orientation?
  • Upload a new version of ios app to app store?
  • What is the most effective way of tracking time/dates in an iOS/Swift application?
  • WatchKit get user's preferred handedness
  • Location permission dialog is shown and immediately disappears
  • Combine these regex expressions
  • How to save an array of HKQuantitySamples (heart rate) to a workout
  • RestKit mapping with parent key as attribute and key contains parenthesis
  • How to add UITextField programmatically to UIScrollView without breaking constraints?
  • How to make a view height grow depending on its child views contents
  • Some users experiencing NSInternalInconsistencyException crash
  • Mark successful siesta response as error
  • Using a modified PDFTron Tools Framework
  • How can I use universal links when I call openURL inside my own app?
  • NSArray of doubles iOS (Objective c)
  • Passing data from embedded PageViewController to parent View Controller
  • Reuse item inside a row when scrolling
  • Realm Relation how to implement
  • error: generic parameter 'T' could not be inferred in swift
  • Adding shadow to UITextView makes text expand out of UITextViewFrame
  • How to change the string in seconds to minutes in Swift3?
  • Search bar is refreshing the index of my table view cell
  • Decodable multiple DateDecodingStrategies
  • The spoiled simulator xcode 9. As a broken TV
  • How to set width of TouchableWithoutFeedback depends on Text in react-native?
  • How to show an UIActivityIndicatorView whitin UITableViewController?
  • UItextfield text in Custom UITableViewCell prints nil when i scroll up in SE
  • How to append or add multiple items to an array in Swift 4
  • SQLite WHERE column MATCH parameter binding
  • Load iframe tag (video) from html string into web view using Swift 3
  • How to determine when a custom UICollectionViewCell is 100% on the screen
  • Get safe area inset top and bottom heights
  • Cordova iOS project: xCode 9 missing some entitlements
  • UITableView not scrolling properly to bottom until a cell is clicked
  • Reloading Particular Row of PickerView in swift
  • How to present a ViewController after pressing a button inside of a CollectionViewCell
  • filtering array of custom view controllers for property?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org