swift set first responder to text field but no cursor is visible - ios

In my swift app, I have a text field, which by default is hidden.
When I press a button, the textfield should become visible and the cursor should be visible together on the keyboard. I am using the following code:
textfield.hidden = false
textfield.becomeFirstResponder()
The text field will become visible and the keyboard will be shown; however, the cursor is only visible if I press on the textfield with my finger.
Where is my error?

i also face the same problem ,i fixed by adding becomeFirstResponder in viewDidAppear instead of viewDidLoad
override func viewDidAppear(_ animated: Bool) { passwordTxtField.becomeFirstResponder() }
After that cursor appears within textfield,another possibility is your Textfield background color is similar to your TextField Tint Color,So changing TextField Tint Color Makes it works

Related

Keep search bar cancel button tint color even when keyboard dismiss

I have a search bar with cancel button in my navigation bar and when keyboard dismisses, my cancel button changes color and become inactive until I click on it again. Is there a way to keep my cancel button state the same every time?
This is before I dismiss keyboard
and this is after I dismiss keyboard
I want my cancel button stay the same before and after keyboard dismissal. Thank you for your help!
I think you you can’t ,the way i did it is made button on the right with a width ,when the user press on search bar,the button width change,and even if the the user search and dissmiss keyboard it still active,but when pressed the width is bacl to zero,and search bar is end edliting
Then when cancel is pressed just make width of it is zero
implementaion:
1-add you a button to the right of tabbar-for constrains for the button:your desired bottom contrain,and width is zero,your desired height,trailing is also zero.
2-for tab bar constrains: trailing must be zero,the rest as you like
3- dont forget to use UISearchBarDelegate
4-in viewdidload set yoursearchbar.delegate = self
5-
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
//wcha.hey = hello.text!
this will fire when tuching search bar for first time
self.allview.layoutIfNeeded()
yourcancelbutton.constant = your desired width
}
6-
#IBAction func yourcancelbutton(_ sender: Any) {
yourbutton.constant = 0
self.view.endEditing(true)
}

Custom keyboard on UITextView shows apple keyboard first then switches to the custom keyboard

Short video of what happens when tapping on a textView:
https://www.youtube.com/watch?v=LG_r3iDJKiM
I'm trying to show my custom keyboard on a UITextView from the textViewDidBeginEditing function, The custom keyboard has been tested on UITextFields and is working as intended. Now I'm trying to get the same keyboard to work with UITextViews.
When opening the app for the first time, and tapping on a textView the first time will have this behavior, then when we tap on other textViews the custom keyboard is there directly as intended.
the Current code in my TextViewDidBeginEditing, I tried changing the order of the last 4 lines but with no success:
public func textViewDidBeginEditing(_ textView: UITextView) {
keyboard = Keyboard.sharedInstance.keyboardWithType(availableKeyboards.numbers.rawValue, returnKey:textView.returnKeyType, isSecure: false,decimalSeparator:self.decimalSeparatorCharacter, responderClass: self)
inputView.keyboardAppearance = .dark
inputView.hideAssistantBar()
inputView.inputView = keyboard
inputView.reloadInputViews()
}
This will result in the video above, I tried some other ways but no success yet:
Setting a empty UIView as the inputView, this will still result in the apple keyboard shortly appearing
let view:UIView = UIView()
textView.inputView = view
also tried doing reloadInputViews but that doesn't work either:
textView.reloadInputViews()
also tried running it all on the main tread with
DispatchQueue.main.async { }
but no success, I cannot understand why even setting a UIView() as the inputView will still shortly show the apple keyboard. Any help in the right direction would be greatly appreciated!
Thanks in advance!

Calling endEditing on a UIView

I have a tableView with two UITextViews. The user can add as many cells as they want by tapping on a "+" button. The tableView starts out with one row (cell). Lets say the user adds some information to the two textViews and then taps the plus button and scrolls to get to it. I call this function to dismiss the keyboard when a user scrolls so it isn't in the way:
override func scrollViewDidScroll(scrollView: UIScrollView) {
self.view.endEditing(true)
}
Now, the keyboard disappears and the user goes to tap on the second row's text field. The tableView jumps up (I think to accommodate the keyboard), but the textField can't be edited (no cursor and keyboard doesn't appear). If I go back up to the first row (row 0), the keyboard appears and I can edit the textView. Why is this happening?
There is a property on scrollView subclasses (e.g TableViews)
tableView.keyboardDismissMode = .OnDrag
Once the user begins a descending drag it dismisses the keyboard
scrollViewDidScroll calls everytime the tableview moves, so when "The tableView jumps up (I think to accommodate the keyboard)" it also calls and close the keyboard. You should add there some confines or put endEditing code in the other place.

Cannot resign searchbar a second time

I really don't understand why this is happening. Any hints would be much appreciated.
Setup:
UIViewController with the following subviews:
UISearchBar
UITextField
UIView (with PickerView and Done button) - viewDidLoad hides it.
I have two helper functions which can be simplified as:
func showPickerView () {
myPickerSubView.alpha = 1
}
func hidePickerView () {
myPickerSubView.alpha = 0
}
(There's actually animation, sliding up and down etc. but that part works fine)
What I want is for myPickerSubView to show when the person taps on the UISearchBar or the UITextField. myPickerSubView is dismissed by the Done button on myPickerSubview.
I started with the UITextField which I got working perfectly with:
func textFieldDidBeginEditing(textField: UITextField)
{
if !(pickerSubViewVisible)
{
self.showPickerView()
}
eventFloorTextField.endEditing(true)
}
func pickerView(eventFloorPicker: UIPickerView, didSelectRow row:Int, inComponent component: Int {
//.. other processing code
self.eventFloorTextField.text = "My Pick: \(myPick)" //so the textfield gets updated
}
I wanted to use a UISearchBar instead because I wanted to uses the built in scope bar for some added functionality. So having set the UISearchBar delegate to self etc. I thought I could just do:
func searchBarTextDidBeginEditing(searchBar: UISearchBar)
{
eventFloorSearchBar.endEditing(true)
self.showPickerView()
}
When you press on the UISearchBar the first time, the PickerSubView shows up as expected. Then, no matter what I do (press on my Textfield, dismiss the PickerSubView or not,) if I tap on the UISearchBar a second time, a keyboard will come up from the bottom. If my PickerSubView is already present, the keyboard will cover it. If the PickerSubView is not present, if I tap on the UITextField, the PickerSubView will slide up behind the keyboard.
I do not know if it is significant - but the caret (blinking cursor) remains in the UISearchBar text field but even if I make it go away by tapping on the UITextfield first, when I tap UISearchBar a second time, it will always be a keyboard showing up.
I've tried resignFirstResponder as well as various other methods, but none of them seem to work.

How to make the bottom toolbar float just above on the soft keyboard when the Textfield get focus

I add a toolbar in bottom of UIViewController and put a TextField on the toolbar, but when I hit the TextField ,the textfield is covered by popup-ed soft keyboard and Can't see it.
So, How to float the toolbar just above on the soft keyboard when the Textfield get focus.
There is a property called inputAccessoryView. When a textField is tapped you can call it like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.inputAccessoryView=self.accessoryToolBar;
return YES;
}
this will call the toolbar on top of your keyboard.

Resources