swift screen move when keyboard appears - swift

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.

Related

A method is launched more than once in swift

I have a class:
class ViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
...
NotificationCenter.default.addObserver(
self,
selector: #selector(self.onDealLaunched),
name: Notification.Name("newDealLaunched"),
object: nil)
}
#objc func onDealLaunched(notification: NSNotification) {
let deal = notification.object as! SimpleSaveGame.deal
let i = projectCollection.count
let indexPath = IndexPath(row: i, section: 0)
let projectDeal: project = project(...)
projectCollection.append(projectDeal)
activeDeals.append(deal)
projectCollectionView!.numberOfItems(inSection: 0)
projectCollectionView.insertItems(at: [indexPath])
projectCollectionView.reloadData()
}
#IBAction func corpoButtonPressed(_ sender: Any) {
let vcCorpo = UIStoryboard(name: "Corpo", bundle: nil).instantiateViewController(withIdentifier: "CorpoViewController") as! CorpoViewController
vcCorpo.currentRound = second
vcCorpo.yearlyTaxIncome = gameVariables[14].value
NotificationCenter.default.addObserver(
self,
selector: #selector(self.onReturnCorpo),
name: Notification.Name("corpoBackButtonPressed"),
object: nil)
self.present(vcCorpo, animated: true, completion: nil)
}
}
Then I have CorpoViewController which present custom UITableView and if a user clicks button it shows popup
class CorpoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...
#objc func onLaunchButtonPressed(notification: NSNotification) { // it is launched when player presses negotiate button
dealId = notification.object as! Int
NotificationCenter.default.removeObserver(self, name: Notification.Name("dealLaunchButtonPressed"), object:dealId)
NotificationCenter.default.addObserver(
self,
selector: #selector(self.onPopupClosed),
name: Notification.Name("negotiationPopupClosed"),
object: nil)
self.negotiatedDeal = self.deals[self.dealId]
popup = NegotiationPopup(dealId: self.dealId, deal: self.negotiatedDeal, corpo: self.corpos[self.deals[self.dealId].corpo])
self.view.addSubview(popup)
}
#objc func onPopupClosed(notification: NSNotification) {
NotificationCenter.default.removeObserver(self, name: Notification.Name("negotiationPopupClosed"), object:nil)
negotiatedDeal = notification.object as! SimpleSaveGame.deal
if (negotiatedDeal.status == 2) {
addActiveDeal(deal: negotiatedDeal)
negotiatedDeal.status = 4
deals.remove(at: dealId)
}
}
override func viewDidLoad() {
super.viewDidLoad()
corpoTableView.delegate = self
corpoTableView.dataSource = self
corpoTableView.register(CorpoCell.self, forCellReuseIdentifier: "cellId")
NotificationCenter.default.addObserver(
self,
selector: #selector(self.onLaunchButtonPressed),
name: Notification.Name("dealLaunchButtonPressed"),
object: nil)
planTimer()
}
}
A class implementing:
class CorpoCell: UITableViewCell {
...
#IBAction func launchButtonPressed(_ sender: UIButton) {
NotificationCenter.default.post(name: Notification.Name("dealLaunchButtonPressed"), object: launchButton.tag)
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
}
And a class representing the popup:
class NegotiationPopup: UIView {
...
#objc fileprivate func animateOut() {
self.removeFromSuperview()
NotificationCenter.default.post(name: Notification.Name("negotiationPopupClosed"), object: self.negotiatedDeal)
}
}
#objc fileprivate func cancelButtonPressed() {
...
animateOut()
}
}
My problem is that when close popup method animateOut() is launched once, but method #objc func onPopupClosed is launched many times. Number of times depends on how many times I close CorpoViewController and then open it again.
Therefore I guess that I have multiple instances of CorpoViewController in memory, but I do not know how to confirm it and how to fix it.
I replaced observer which indicated that popup was closed with delegate and it fixed the problem somehow.

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

View above keyboard don't show up

I want to have a View with a textField an a send button above my keyboard, when the keyboard is shown. But this doesn't work.
I already implemented this in my code:
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tabBarController?.tabBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
tabBarController?.tabBar.isHidden = false
}
// MARK: - Keyboard stuff
#objc func keyboardWillShow(_ notification: NSNotification) {
let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
print("hier ist \(keyboardFrame!)")
UIView.animate(withDuration: 0.1) {
self.bottomConstraint.constant = keyboardFrame!.height
self.view.layoutIfNeeded()
}
}
#objc func keyboardWillHide(_ notification: NSNotification) {
UIView.animate(withDuration: 0.1) {
self.bottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
Check out textField inputAccessoryView. There's a tutorial here that explains what you're trying to do.

Unrecognized selectors sent to instance, Notification center getting the Keyboard size

If I try to tap into my textfields I get an error, related to these few lines of code that try to get the size of the keyboard on a mobile ios device. The Notification Center lines of code are inside the overriding ViewDidAppear.
NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillShow:")), name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillHide:")), name: UIResponder.keyboardDidHideNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.bottomConstraint.constant = keyboardSize.height
}
}
}
func keyboardWillHide(notification: NSNotification) {
self.bottomConstraint.constant = 0
}
Use the type safe syntax
#selector(keyboardWillShow)
and
#objc func keyboardWillShow(_ notification: Notification) { ...
However I highly recommend to use the modern closure based syntax
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { [weak self] notification in
if let userInfo = notification.userInfo,
let keyboardSize = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self?.bottomConstraint.constant = keyboardSize.height
}
}
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { [weak self] _ in
self?.bottomConstraint.constant = 0
}
Try Following code :
override func viewDidLoad() {
super.viewDidLoad()
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: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("notification: Keyboard will show")
}
}
#objc func keyboardWillHide(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
}
}
You can try this:
This code in viewDidLoad():
// Do any additional setup after loading the view.
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)
and then add this in ViewController
#objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("Keyboard opened \(keyboardSize)")
}
}
#objc func keyboardWillHide(notification: NSNotification) {
print("Keyboard hide")
}
Hope this will help.
You should deregister any notification you register in a view.
func registerForKeyboardNotifications()
{
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
func deRegisterFromKeyboardNotifications()
{
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
#objc func keyboardWasShown(_ notification: NSNotification)
{
//todo
}
#objc func keyboardWillBeHidden(_ notification: NSNotification)
{
//todo
}
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyboardNotifications()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
deRegisterFromKeyboardNotifications()
}
You've got this error because of notification parameter. With current signature you should use:
#selector(keyboardWillShow(notification:))
#selector(keyboardWillHide(notification:))
Or rewrite your methods in that way:
#objc func keyboardWillShow(_ notification: Notification) {
// Code
}
#objc func keyboardWillHide(_ notification: Notification) {
// Code
}
And use the next syntax:
#selector(keyboardWillShow(_:))
#selector(keyboardWillHide(_:))
Edited:
You can also use simplified syntax:
#selector(keyboardWillShow)
#selector(keyboardWillHide)
Use like below code
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow(notification:)),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide(notification:)),
name: UIResponder.keyboardWillHideNotification,
object: nil)
#objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
// Your code
}
}
#objc func keyboardWillHide(notification: NSNotification) {
// Your code
}
Hope this works, If any doubt plz comment.