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
}
}
Also in Swift:
- number of characters in a string swift
- swift scrollview hide scrollbar
- swift open web page
- receive notificationcenter data in swift
- swift print struct name
- Resizing UITableView to fit content
- xcode create image from calayer
- swift initialize a view
- navigationcontroller.pushviewcontroller
- swift and xcode and convert "Int to Int16"
- white or light ASAuthorizationAppleIDButton “Sign in with Apple” button - Swift
- swift loop through array
- using swipe gesture recognizer swift
- swift get current time
- How to find index of list item in Swift?
- generate random bool swift
- swift generate uuid
- UI API called on a background thread
- swift add horizontal line uiview programmatically
- swift allow gesture recognizer over others
- type String and int swift addition
- ionic Library not found for -lGoogleToolboxForMac
- how to set return type swift
- upload a single image alamofire 5.0 swift