Remove focus from the textfield - ios

I am doing two operations on a textfield:
1) To move the textfield using pan gesture.
2) To write on the textfield when the user clicks on it.
Concern is that I want to perform one operation at a time i.e when the user is writing he cannot scroll and vice versa.
Name of UIButton action --- button
Name of UIButton outlet --- optionButton
- (IBAction)button:(id)sender
{
if([_optionButton.titleLabel.text isEqualToString:#"SCROLL"])
{
NSLog(#"can scroll");
NSLog(#"POINT=============%d ",point);
point=0;
[_optionButton setTitle:#"WRITE" forState:UIControlStateNormal];
textField1.enabled=NO;
}
else
{ NSLog(#"can write");
NSLog(#"POINT=============%d ",point);
point=1;
[_optionButton setTitle:#"SCROLL" forState:UIControlStateNormal];
textField1.enabled=YES;
}
}
The point variable is controlling the complete operation and is set to 1 at the beginning.This means that the user can write in the beginning and if he want to write he has to press the button.
The problem that I am facing is that by using "textField1.enabled=NO" I am not able to scroll the textfield.
Is there any function using which I can remove the focus from the textfield but can scroll it.

You can use this code to remove focus (As said in one of the comment),
[textField1 resignFirstResponder];

you can also remove focus from text field by calling [textField1 endEditing:YES]

An UITextfield only has 1 line, so you canĀ“t scroll it.
UPDATE
If you want to move your UITextfield with your finger use UIPanGestureRecognizer in your parent view and apply the changes in your uitextfield.

You can simply follow the steps:
Implement UITextFieldDelegate into your Controller
class ClassName: UIViewController, UITextFieldDelegate {....}
Implement the "textFieldShouldBeginEditing" function
class ClassName: UIViewController, UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
}
So returning false in textFieldShouldBeginEditing will simply not allow to edit into the UITextField.

Related

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];
}

a lot of button add same addTarget(_:action:for:) method

I'm working on a calculate app.
and has ton of button which I store in a big stackView (created in SB and no outlet).
each button cast some shadow (also set in SB attribute).
I want to get rid of shadow when button was pressed.
either tapGestureRecognizer or target action could only effect one UIButton.
any convenience way to acheive
PS I mean when button .touchupinside or tapGestureRecognizer .end .start when finger move button should still cast the shadow
help appreciated
UIButton will highlighted on click, so check button setting Change the title color in highlight state config to same as default state Or you can set:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
If you want to control Highlighted by code, you can disable normal highlighted by subclass Button and disable in touchesBegin:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.state == UIControlStateHighlighted) {
[self setHighlighted:NO];
}
}
You can assign the same IBAction to multiple buttons, so that the same method is called for all of them
func buttonDidTouch(sender:UIButton) {}
If now you want to identify which exactly button is being called, you can use the UIButton.tag property to identify it. The tag can be set in the SB for each button.
There are so many way to achieve the goal but I'm going with below.
Just give my logic.
Take one temporary optional variable of UIButton
Ex.
var myTemButton: UIButton()?
In the button action method
#IBAction func myButtonActionFunc(_ sender: UIButton) {
if myTemButton == nil {
myTemButton = sender
}
else {
// Use "myTemButton" and write here code for remove shadow Or other stuff that you want.
}
// use "sender" and write code here for apply shadow of button or other stuff that you want.
}

When viewDidLoad set textField editable / enable keyboard

I have an app with a UITextField. When the containing view appears, I want a keyboard to slide up and begin editing this text field. Not sure how I would go about doing this?
I've tried:
func viewDidLoad {
textField.editing == true
textField.selected == true
In viewWillAppear add a call to becomeFirstResponder, something like this:
[textField becomeFirstResponder];
or in Swift:
textField.becomeFirstResponder()

resignFirstResponder programmatically [duplicate]

This question already has answers here:
Understanding resignFirstResponder with UITextField
(3 answers)
Closed 9 years ago.
How do I remove a keyboard from the view as a result of the return key being touched when the UITextField was created programmatically.
If the UITextField was called in the viewDidLoad I know how to do this, but the UITextField was created as a result of an -(IBAction).
I created my UITextField programmatically. I know the resignFirstResponder removes the keyboard. I have it set up to do so when the screen is taped outsie the keyboard. I also have it working to where if the user triggers the IBAction with the UIButton related to the UITextField the keyboard goes away. I also want to be able to hide the keyboard when the user selects return from the keyboard.
You need to make yourself a UITextFieldDelegate and implement:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Make sure you set the textField's delegate to self when you create it.
You can use this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
You'll need to set your text field's delegate to self first, though:
self.textField.delegate = self;
Or, you could right-click-drag from the text field in IB to little orange circle at the bottom.

How to know when UITextView became first responder

How to handle when uitextview became first responder. I have text in text view and I want when the view became active want to clear the text. How can I do that? Thanks in advance
You can definitely use a UITextViewDelegate method of:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
Just return YES and intercept inside that method. You can also do it for UITextFields with UITextFieldDelegate and:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
textViewShouldBeginEditing actually triggers before the text view becomes the first responder.
textViewDidBeginEditing will trigger once the text view becomes the first responder and would be the best place to execute code that needs to know what the active textview is.
If you are not really worried about which field is active and just want to clear the text once the field is tapped on you could use either function.
EDIT: The same methods are available for text fields.
As above, you can override becomeFirstResponder but note that you must call the superclass implementation. If you don't, things like popping the keyboard on a text field won't work. i.e.
override func becomeFirstResponder() -> Bool {
if super.becomeFirstResponder() {
// set up the control state
return true
}
return false
}
The Swift 4 solution
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}
Previous answers do the job great for UITextBox, but if you have a custom class derived from NSResponder and need to know when it becomes first responder:
-(BOOL) becomeFirstResponder
{
// Your stuff here
return YES;
}

Resources