I want to implement that function into my textfield's function. I tried it different ways but I couldn't succeed.
Here is the code:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
#objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height
}
}
}
You're using key UIResponder.keyboardFrameBeginUserInfoKey which cause that you get wrong height.
Use this key instead:
UIResponder.keyboardFrameEndUserInfoKey
Related
I'm moving up the view when the keyboard appears.
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: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
#objc func keyboardWillHide(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y += keyboardSize.height
}
}
}
But when changing the language by clicking up globe icon at the bottom of the keyboard and when the language is not English it appears a black line.
Is there a way to prevent this black lilne while showing suggestions for English and not for other languages?
I'm guessing the problem is that the keyboard is actually changing its frame when you switch to another layout.
Register to UIResponder.keyboardDidChangeFrameNotification and update your frame accordingly.
More info on this notification are available here: https://developer.apple.com/documentation/uikit/uiresponder/1621619-keyboarddidchangeframenotificati
i try to create a Chat UI. Therefore i added a Tableview and a textfield below. If a user wants to type in the textfield, the whole view should go up and thats work fine. If the user ends typing and the keyboard should be hidden, the first time the view moves a bit lower and the seccond time it moves lower as well. It looks like the following screenshots:
is used the following code:
override func viewDidLoad() {
super.viewDidLoad()
self.myChat.separatorStyle = .none
getChatId()
myChat.allowsSelection = false;
newMessage.delegate = self
// Do any additional setup after loading the view.
NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
#objc 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
}
}
}
#objc 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-1
}
}
}[![normally it should look like this][1]][1]
regards
Change this function with my change.
#objc 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 = 0
}
}
}
I have general message view controller I am sending message using textfield but keyboard is covered the text fields. I can refer
Move view with keyboard using Swift
this link added some code but after that some portion shows black. how to avoid the screen black after sending the message
this is code
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(GenralMessageViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(GenralMessageViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
#objc 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
}
}
}
#objc 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
}
}
}
Your code is just wrong. You have no business changing the frame of self.view. The main view of a view controller must be left in place where it is. Wrap your interface in a subview of the main view and move the subview (if you insist on using this approach to avoiding being covered by the keyboard).
Try changing your end y origin back to 0 instead of a calculated value.
#objc 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 == 0
}
}
}
my code is working fine for the first time when clicking the button I got the keyboard height but the problem is when it dismisses and I call it again it returns hight 0, any help here is the code
#IBAction func showCaseProgressSliderBtn(_ sender: Any){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
keyboardHeightCG = keyboardHeight
print("keyboard is \(keyboardHeightCG)")
}
}
what about to check it with UIKeyboardFrameEndUserInfoKey wouldn't this work?
I have six UITextFields in my view and I want to decide whether the view has to move or not. How can I check which TextField is selected before moving my view?
Thats my Code for showing the keyboard and moving the view:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y -= keyboardSize.height - 85
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y += keyboardSize.height - 85
}
}
If you want to know which UITextField is selected, you can use textFieldDidBeginEditing and textFieldDidEndEditing
func textFieldDidBeginEditing(textField: UITextField!) {
currentSelectedTextField = textField
}
func textFieldDidEndEditing(textField: UITextField!) {
currentSelectedTextField = nil
}
For your reference about how to manage keyboard when selecting a UITextField: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html