Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

ios uikit hide/show keyboard if scrolling

func keyboardWillShow(notification: NSNotification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height
        }
    }

}

func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}
Comment

ios uikit hide/show keyboard if scrolling

// ViewController.swift
class ScrollViewController: UIViewController {

  @IBOutlet weak var scrollView: UIScrollView!
    
  override func viewDidLoad() {
    super.viewDidLoad()

      // Do any additional setup after loading the view.
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
  }
  
  @objc func keyboardWillShow(notification: NSNotification) {
    guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
    else {
      // if keyboard size is not available for some reason, dont do anything
      return
    }

    let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height , right: 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
  }

  @objc func keyboardWillHide(notification: NSNotification) {
    let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
		
    
    // reset back the content inset to zero after keyboard is gone
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
  }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: Swift Labeled Statement with continue 
Swift :: key path expression as functions ios swift 
Swift :: ln -s ~/.platformio/penv/bin/platformio /usr/local/bin/platformio ln -s ~/.platformio/penv/bin/pio /usr/local/bin/pio ln -s ~/.platformio/penv/bin/piodebuggdb /usr/local/bin/piodebuggdb 
Swift :: Swift Add Elements to a Set 
Swift :: swift 5 cancel or end block operation 
Swift :: Swift Function Overloading 
Swift :: mp3 player with swift iOS & macOS 
Swift :: Swift Create Multiple Objects of Class 
Swift :: Swift Nested for Loop 
Swift :: swift variable 
Swift :: how to figure out ios vesion swift 
Ruby :: how to remove columns from rails 
Ruby :: ruby variable in string 
Ruby :: ruby calculate execution time 
Ruby :: getting wanked off by ruby 
Ruby :: how to create migration with uniqueness inrails 
Ruby :: shopify cart show total save 
Ruby :: ruby exponent 
Ruby :: rails find_by order 
Ruby :: how to delete database in rails 
Ruby :: ruby median find 
Ruby :: how to remove nested array brackets ruby 
Ruby :: how add an index column in rails 
Ruby :: ruby hash delete 
Ruby :: write csv with header ruby 
Ruby :: how to destroy a generate in rails 
Ruby :: how to upload a file in capybara spec 
Ruby :: ruby includes 
Ruby :: force stop rails server 
Ruby :: ruby rspec change matcher 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =