Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swift view float on keyboard show

//swift 4.2 and above "5.3 tested"
override func viewDidLoad() {
    super.viewDidLoad()            
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height
          //we can also change constraints runtime
        }
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
      //this area will set old constraints again
    }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: print document directory path swift 
Swift :: convert string to int swift 
Swift :: swift 5 btn change image 
Swift :: swift enum all cases 
Swift :: swift rotate text 90 degrees 
Swift :: random element from array swift 
Swift :: gap between table header uitableview 
Swift :: how to add social media icons in swiftui 
Swift :: check when webview finish loading swift 
Swift :: swift get top constraint 
Swift :: round up swift 
Swift :: swift push view controller 
Swift :: swipe to delete xcode 
Swift :: hide status bar in tableview cell in swift 
Swift :: append new element to dictionary in swift 
Swift :: create dictionary swift 
Swift :: navigation title bar color swftui 
Swift :: array swift 
Swift :: swift add width height constraint to view without a lot of code 
Swift :: count down timer swift stack overflow 
Swift :: swift dictionary get key from valye 
Swift :: swift multiline string 
Swift :: Swift Create an Empty Set 
Swift :: setting a local notification at specific time every day swift 
Swift :: uibutton swift set title 
Swift :: button swift ui 
Swift :: Modifying Value Types from Method Swift 
Swift :: swift 5 flatMap wtih keypath 
Swift :: var i = 2 repeat { i *= i * 2 } while i < 100 print(i) 
Swift :: swift UIColor to String 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =