I am trying to build an input screen for the iPhone in swift. The screen has a number of input fields. Most of them on the top of the screen, but three fields are at the bottom. When the user tries to edit the text on the bottom of the screen, the keyboard will pop up and it will cover the screen. I found a simple solution to move the screen up when this happens, but the result is that the screen always moves up and the fields on top of the screen move out of reach when the user tries to edit those.
for example : I want form txt1 to txt4 the view does not go up.
Is there a way to have the screen only move when the bottom fields are edited?
I have used this code I found here:
override func viewDidLoad() {
txt1.delegate = self
txt2.delegate = self
txt3.delegate = self
txt4.delegate = self
txt5.delegate = self
txt6.delegate = self
txt7.delegate = self
txt8.delegate = self
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)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height
}
}
}
#objc func keyboardWillHide(notification: NSNotification) {
if view.frame.origin.y != 0 {
self.view.frame.origin.y = 0
}
}
**
I did this function to start show keyboard while I start editing on textfields:
public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
but here it's going for all the textfield that I have and its the same problem.
how can I make it for specific textfield?
Related
I'm trying to update the constraints of my view when the keyboard appears and disappears
I have notifications for keyboardWillShow and keyboardWillHide and corresponding functions to call them:
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 private func keyboardWillShow(notification: NSNotification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardFrameEnd = keyboardFrame.cgRectValue
let convertedKeyboardFrameEnd = self.view.convert(keyboardFrameEnd, from: nil)
let intersection = convertedKeyboardFrameEnd.intersection(listView.frame)
if intersection.size.height > 0 {
self.listView.snp.makeConstraints { make in
make.top.leading.trailing.equalTo(0)
make.bottom.equalTo(-intersection.height)
}
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
}
#objc private func keyboardWillHide() {
self.listView.snp.makeConstraints { make in
make.edges.equalTo(0)
make.bottom.equalTo(0)
}
self.listView.setNeedsUpdateConstraints()
self.listView.updateConstraints()
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
It seems that keyboardWillShow gets called and updates the size of my listView to be the visible size outside of the keyboard. However, keyboardWillHide gets called and it doesn't seem to update the bottom back to have value 0 so I end up with a case where my view is not resized back to its original size. Any ideas what might be wrong here?
so I have a small app where I choose an image, get it into an image view
and I have 2 textfield to insert a funny phrase
(Meme editor app)
my problem is since the bottom textfield is covered when the keyboard is shown I had to shift the view upwards every time the keyboard is shown for the bottom texfield and I succeed in doing that, what goes wrong is that every time I re-tap the beginning or the end of an existing text in the text filed the view shifts up again in undesirable behavior
here is a small GIF that shows what happens exactly
here is my code so far:
Function to get Kyboard height:
func getKeyboardHeight(_ notification:Notification) -> CGFloat {
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue // of CGRect
return keyboardSize.cgRectValue.height
}
Function to shift the view up in the condition that bottom textfield is what the user taps
#objc func keyboardWillShow(_ notification:Notification) {
if bottomTextField.isFirstResponder{
view.frame.origin.y -= getKeyboardHeight(notification)
}
}
Function to return the view to its normal position when the user finishes editing the bottom text field
#objc func keyboardWillHide(_ notification:Notification) {
view.frame.origin.y = 0
}
Functions to add and remove observers of keyboard notifications
func subscribeToKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
func subscribekeyboardWillHide() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
func unsubscribeFromKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
}
func unsubscribekeyboardWillHide() {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
and where I call them
override func viewWillAppear(_ animated: Bool) {
super .viewWillAppear(true)
cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
subscribeToKeyboardNotifications()
subscribekeyboardWillHide()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
unsubscribeFromKeyboardNotifications()
unsubscribekeyboardWillHide()
}
if you could be kind to provide a simple explanation for your solution I would appreciate it
This is what I do while managing keyboard. It causes no problems.
Declare in your UIViewController class
private let notificationCenter = NotificationCenter.default
then in
override func viewDidLoad() {
super.viewDidLoad()
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
This is the adjust for keyboard function
#objc private func adjustForKeyboard(notification: Notification) {
guard let userInfo = notification.userInfo else { return }
guard let keyboardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
scrollView.contentInset = UIEdgeInsets.zero
} else {
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
scrollView.scrollIndicatorInsets = scrollView.contentInset
}
Deinit here -
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(true)
notificationCenter.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
Every time you change the cursor location, UIResponder.keyboardWillShowNotification notification triggered, thats why every time you tap the textfield it moves up one keyboard height more.
You can use UIResponder.keyboardDidShowNotification notification instead of UIResponder.keyboardWillShowNotification. This one is not triggered when cursor location changes.
This is my view.
When I click inside the text view the keyboard was coming on top. so I added made a class and in that class, I added these functions.
var objectObserver:UIViewController?
func setKeyboardResponsiviness(observer:UIViewController){
objectObserver = observer
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)
}
#objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if objectObserver!.view.frame.origin.y == 0 {
objectObserver!.view.frame.origin.y -= keyboardSize.height
}
}
}
#objc func keyboardWillHide(notification: NSNotification) {
if objectObserver!.view.frame.origin.y != 0 {
objectObserver!.view.frame.origin.y = 0
}
}
After adding the code the whole screen slides up which was the intended goal but as a side effect, half of the text view is out of the screen. Any idea how I can fix this?
The simple solution is to use IQKeyboardManagerSwift.
pod 'IQKeyboardManagerSwift' // add this in your pod file.
Add the following code in didFinishLaunchingWithOptions.
IQKeyboardManager.shared.enable = true
I hope this helps.
In my Collection ViewController I have Collection View Cell and textField in them. Every time I press on textField and keyboard shows up the view is moved up leaving white space...
Before keyboard is showed
everything is ok
After keyboard is shown
cell layout is up... and you cannot see the entire cell content.
I tried one answer solution below and I got this: black view on top
You can add observers for when the keyboard shows and hides and shift your views accordingly:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let newYOrigin = CGFloat(100) // or whatever new value you want
self.yourView.frame.origin.y = newYOrigin
}
}
#objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let newYOrigin = CGFloat(0) // or whatever new value you want
self.yourView.frame.origin.y = newYOrigin
}
}
I wasn't able to find a solution here for my problem which seems to be just that little step above the regular "move up view when keyboard appears" thing.
Although this is exactly what I need I want this only to happen on an iPhone 5S or iPhone SE.
So I've got the following code so far which is working fine with iOS 11:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
#objc func keyboardWillHide() {
self.view.frame.origin.y = 0
}
#objc func keyboardWillChange(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if myTextField.isFirstResponder {
self.view.frame.origin.y = -keyboardSize.height
}
}
}
I've tried several things to put this into an if case and I've searched for how to define things only for types of devices but as a beginner (6th week learning...) I've totally failed so far.
After #rmaddy gave me the right hint I'm giving the answer myself. All I did was adding an if case checking the view size with view.bounds.height (which is 568pt for an iPhone 5).
So the above code was changed to:
override func viewDidLoad() {
super.viewDidLoad()
let screenHeight = view!.bounds.height
//Checking the view size in pt here
if screenHeight <= 568 {
//add this to perform only for devices with view size 568 or lower
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
}
#objc func keyboardWillHide() {
self.view.frame.origin.y = 0
}
#objc func keyboardWillChange(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if myTextField.isFirstResponder {
self.view.frame.origin.y = -keyboardSize.height
}
}
}