Binding a button (UIView) to keyboard that works in multiple textfields (Swift 4.2)
up vote
0
down vote
favorite
Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.
I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.
picture: Initial view without keyboard.
picture: keyboard has been launched.
My UIView
extension:
import UIKit
extension UIView{
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification){
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
}
and I called bindToKeyboard()
to loginBtn
in the viewDidLoad()
of my LoginVC like so:
loginBtn.bindToKeyboard()
The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.
The point of my question:
- how can I implement the extension to be able to work properly with multiple textfields/textviews?
- If that's not possible, how should I approach this problem?
I am sorry if my explanation and or English is unclear.
Thank you so much.
ios swift uiview swift4.2
add a comment |
up vote
0
down vote
favorite
Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.
I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.
picture: Initial view without keyboard.
picture: keyboard has been launched.
My UIView
extension:
import UIKit
extension UIView{
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification){
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
}
and I called bindToKeyboard()
to loginBtn
in the viewDidLoad()
of my LoginVC like so:
loginBtn.bindToKeyboard()
The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.
The point of my question:
- how can I implement the extension to be able to work properly with multiple textfields/textviews?
- If that's not possible, how should I approach this problem?
I am sorry if my explanation and or English is unclear.
Thank you so much.
ios swift uiview swift4.2
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.
I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.
picture: Initial view without keyboard.
picture: keyboard has been launched.
My UIView
extension:
import UIKit
extension UIView{
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification){
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
}
and I called bindToKeyboard()
to loginBtn
in the viewDidLoad()
of my LoginVC like so:
loginBtn.bindToKeyboard()
The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.
The point of my question:
- how can I implement the extension to be able to work properly with multiple textfields/textviews?
- If that's not possible, how should I approach this problem?
I am sorry if my explanation and or English is unclear.
Thank you so much.
ios swift uiview swift4.2
Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.
I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.
picture: Initial view without keyboard.
picture: keyboard has been launched.
My UIView
extension:
import UIKit
extension UIView{
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification){
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
}
and I called bindToKeyboard()
to loginBtn
in the viewDidLoad()
of my LoginVC like so:
loginBtn.bindToKeyboard()
The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.
The point of my question:
- how can I implement the extension to be able to work properly with multiple textfields/textviews?
- If that's not possible, how should I approach this problem?
I am sorry if my explanation and or English is unclear.
Thank you so much.
ios swift uiview swift4.2
ios swift uiview swift4.2
asked 19 hours ago
Sean Saoirse
33
33
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
How about try to implement same login button with keyboard inputAccessoryView?
Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.
New contributor
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– CGPA6.4
4 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
How about try to implement same login button with keyboard inputAccessoryView?
Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.
New contributor
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– CGPA6.4
4 hours ago
add a comment |
up vote
0
down vote
How about try to implement same login button with keyboard inputAccessoryView?
Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.
New contributor
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– CGPA6.4
4 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
How about try to implement same login button with keyboard inputAccessoryView?
Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.
New contributor
How about try to implement same login button with keyboard inputAccessoryView?
Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.
New contributor
New contributor
answered 18 hours ago
HUNG IKAI
1
1
New contributor
New contributor
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– CGPA6.4
4 hours ago
add a comment |
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– CGPA6.4
4 hours ago
1
1
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– CGPA6.4
4 hours ago
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– CGPA6.4
4 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343726%2fbinding-a-button-uiview-to-keyboard-that-works-in-multiple-textfields-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown