How to move StatView programmatically when keyboard shows - swift

I have a login page which has two textviews and a button in a stackView
I'm trying to move the stackview when the keyboard shows and also disappears as I want to know how to do this programmatically

iOS sends 2 notifications when keyboard will show/hide
UIKeyboardWillShow
UIKeyboardWillHide
What you can do, is observe those notifications, and move the frame of your stackView such as
#objc func keyboardWillShow() {
if stackView.frame.origin.y == 0 {
stackView.frame.origin.y -= 200
}
}
#objc func keyboardWillHide() {
if stackView.frame.origin.y != 0 {
stackView.frame.origin.y = 0
}
}
And here is how to observe those notification. (use this code in your viewDidLoad function)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: Notification.Name.UIKeyboardWillHide, object: nil)

Swift 4.2
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIWindow.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIWindow.keyboardWillHideNotification, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification) {
print("keyboardWillShow")
}
#objc func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
}
If still got any error check below link:
LINK FOR KEYBOARD HIDE/SHOW

Related

App crashing when adding completion to the observer

I'm trying to add a block to the notification observer, but the app is getting crashed when moving foreground and background. If I'm adding method to observer its working fine, only in case of block its getting crashed. Here is my code which I'm trying for the same.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(foregroundEntered(closure:)), name: UIApplication.willEnterForegroundNotification, object: nil)
// Do any additional setup after loading the view.
}
#objc func foregroundEntered(closure: () -> Void) {
/// do some stuff
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
}
}
Thanks in advance.
Replace
#objc func foregroundEntered(closure: () -> Void) {
With
#objc func foregroundEntered(_ notif: NSNotification) {
And change
NotificationCenter.default.addObserver(self, selector: #selector(foregroundEntered), name: UIApplication.willEnterForegroundNotification, object: nil)

Subviews does not follow constraints and dismissing keyboard leaves a black space at the bottom of the view

Whenever my root view controller(Navigation Controller) pushes to my 2nd VC (let's call it VC2), all of VC2's subviews: buttons, labels, and text fields get shifted down even though I've set constraints on it. Also, whenever I dismiss the keyboard after editing the text field, the view goes back to its intended place where the constraints set it to be and leaves a black space at the bottom of the view. Here is my code for VC2:
override func viewDidLoad() {
super.viewDidLoad()
view.layer.backgroundColor = UIColor(named: const.white)?.cgColor
nameField.delegate = self
emailField.delegate = self
passField.delegate = self
setUpTitles()
setUpTextFields()
setUpButton()
addPaddingAndBorder(to: nameField)
addPaddingAndBorder(to: emailField)
addPaddingAndBorder(to: passField)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeView(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardIsDone(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeView(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard(_:)))
self.view.addGestureRecognizer(tap)
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
#objc func keyboardWillChangeView(notification: Notification){
view.frame.origin.y = -150 //This is temporary
}
#objc func keyboardIsDone(notification: Notification){
view.frame.origin.y = 0
}
#objc func dismissKeyboard(_: UITapGestureRecognizer){ //Tapping outside the keyboard
nameField.resignFirstResponder()
emailField.resignFirstResponder()
passField.resignFirstResponder()
}
//Executed when hitting return key
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
nameField.resignFirstResponder()
emailField.resignFirstResponder()
passField.resignFirstResponder()
view.frame.origin.y = 0
return true
}
Before:
After:
When you use responders you should make your first textfield to be a first responder, like that:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
firstTextField.becomeFirstResponder()
}
This is my answer in different issue

App crashing: Exception - unrecognised selector sent to instance

I've got a UITableviewController with following logic to slide up / down the entire view once the keyboard toggles like so:
class ChatDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// variables...
override func viewDidLoad() {
super.viewDidLoad()
// do stuff...
NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillShow:")), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillHide:")), name: UIResponder.keyboardWillHideNotification, object: nil)
// do other stuff...
}
...
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y -= keyboardSize.height
}
}
func keyboardWillHide(notification: NSNotification) {
self.view.frame.origin.y = 0
}
...
}
Toggling the keyboard then crashes the App with following exception: ChatDetailViewController keyboardWillShow:]: unrecognized selector sent to instance 0x7f82fc41fdf0
The error message seems clear at first glance but I still can't figure out what's wrong with my selector. No code warnings, no typos,...
What is it I'm doing wrong here?
In Swift 4 i have previously used in like this
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification) {
print("keyboardWillShow")
}
#objc func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
}
In Swift 5 they renamed the way to access the keyboard notifications to go through UIResponder:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
Three issues:
The creation of the selector is wrong (use #selector)
The signature of the action is wrong (missing underscore)
The action must be marked as #objc
And the type in Swift is Notification without NS prefix
override func viewDidLoad() {
super.viewDidLoad()
// do stuff...
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
// do other stuff...
}
...
#objc func keyboardWillShow(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y -= keyboardSize.height
}
}
#objc func keyboardWillHide(_ notification: Notification) {
self.view.frame.origin.y = 0
}
...
You forgot to add #objc part:
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(_ sender: Notification) {}
#objc func keyboardWillHide(_ sender: Notification) {}

viewWillEnterForeground issue

xcode10.1
swift4.2
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.viewWillEnterForeground(_:)), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}
I used to use code above to run method when the app enter foreground. However, this code does not run anymore. There is no UIApplicationWillEnterForeground after NSNotification.Name.
Anyone knows how to add notification method when the app enter foreground in VC?
Add notification observer in viewDidLoad of the view controller
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.viewWillEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
}
#objc func viewWillEnterForeground(_ notification: Notification) {
//Work anything app enter in background
}
The post-notification fire from app delegate willEnterForgroundMethod
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
NotificationCenter.default.post(name: UIApplication.willEnterForegroundNotification, object: nil)
}

swift screen move when keyboard appears

I have a textview and a text field on my screen and I want move screen when edit textview.
This is my code
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
// NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)
print("addd observer")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
// NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}
#objc func keyboardWillShow(_ sender: Notification) {
let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
print("keyboardwillshow")
ksize = keyboardSize?.height
}
#objc func textViewDidBeginEditing(_ textView: UITextView) {
print("textviewedit")
boxview.frame.origin.y -= ksize
print("textview")
}
#objc func textViewDidEndEditing(_ textView: UITextView) {
print("textvieweditend")
boxview.frame.origin.y = 0
}
It works when I just edit textview. However, if I edit the textview right after focus on the textfield(without exit keyboard) it doens't work.
I already checked that textviewDidBeginEditing() is called but the screen doesn't move.. How can I solve this? All advises are welcome!
I add self.view.Layoutifneeded() to keyboardwillchange() and it works!! thanks everybody.