Cannot resign searchbar a second time - ios

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.

Related

How to fix highlighted state UIButton delay in UIPageViewController page?

I have a screen in which I have a UIPageViewController in each page I have a UIButton. The problem is that there is a delay of the pressed/highlighted state of the button for about a half second when the user presses the button. both state images are set to the button using the storyboard.
This happens in the Simulator as well as on a real device.
Now from my Google searches, I came across a few posts that describe this issue, for example:
UIButton delayed state change
and:
UIbutton only looks clicked (highlighted) on longPress?
In all posts, the solution is to use the delaysContentTouches setting and set it to false.
The problem is: I didn't found how would I apply this in my case of a UIPageViewController. most of the posts talk this issue in a UIScrollView or a UITableView.
So, the question is: how would I do that in case of a UIPageViewController? I didn't see that UIPageViewController has this setting and didn't find any other way to apply it.
Found a solution to this issue, This piece of code will fix the button highlighted click delay but will prevent the pager scroll on the button itself.
public override func viewDidAppear(_ animated: Bool) {
for view in self.view.subviews {
if view is UIScrollView {
(view as? UIScrollView)!.delaysContentTouches = false
}
}
}
The reason I didn't find this in UIPageViewController is that UIPageViewController is not a subclass of UIScrollView as I expected but it contains it as a subview.

IQKeyBoardManager : Hide keyboard for a specific textfield

In my app I have a form where the user fills out his/her information, the form has some textfields and amongst those is one where the user can select their country. This textfiled is a dropdown which when active displays a dropdown (tableview) with the list of countries to select. I have created a demo project, to reproduce the issue and share it here.
In this demo, I have add 3 textfields, when the user goes to the second textfiled, using the next button on the keyboard, a tableview is displayed. Now what I want to achieve is my keyboard should hide when the dropdown tableview is displayed.
I have put in quite some time trying to figure this out but has no success.
Here is the link to the demo project:
https://github.com/tejaskutal/IQKeyboardManagerDemo
Any help is greatly appretiated.
P.S: For some weird reason, when you click next when the first textfiled is active, it jumps to the third one first and then goes to second textfield after next is clicked one more time. However, this has nothing to do with the original problem so I left it there.
No need to handle different delegate. In your viewDidload you can do something like,
yourTextField.inputView = UIView.init(frame: CGRect.zero)
yourTextField.inputAccessoryView = UIView.init(frame: CGRect.zero)
so, for that textfield keyboard or toolbar will not be appeared.
You can use your UITextField's delegate and implement the textFieldShouldBeginEditing. Something like
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == theTextFieldToIgnore {
view.endEditing(true)
return false
}
}
As for your textField chaining, you should implement the textFieldShouldReturn
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == firstOne {
textField.resignFirstResponder()
secondOne.becomeFirstResponder()
}
return true
}
Return key Action which textfield you want to resign
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.yourTf resignFirstResponder];
}

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!

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

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

Keyboard blinks when moving to Next Responder

I implemented basic Next button on the Text Field using the following code:
func textFieldShouldReturn(textField: UITextField) -> Bool {
let didResign = textField.resignFirstResponder()
if(!didResign) {
return false
}
let myView = getParentView(textField)
dispatch_async(dispatch_get_main_queue(), { myView.nextField?.contentTextField.becomeFirstResponder() })
return true
}
What annoys me is that before nextField becomes First Responder, the keyboard disappears for a brief moment which looks ugly.
The effect is not always visible, sometimes it looks like the keyboard stayed up, sometimes it blinks fast.
Is there a way to resign first responder but still keep the keyboard up?
Move to next field whilst keeping the keyboard?
If you're working with UI, you should be on the main queue, so there's no need to dispatch_async to the main queue. Removing this code will make your code cleaner.
The method you're implemented determines the behavior of the app when the return key is pressed. Resigning first responder dismisses the keyboard, so if this is not the effect you want, don't resign first responder.

Resources