I use the code below. As it seems I have 5 uitextfields. Now for some reason when I clicked next, the view actually scrolled down automatically so I could see the textfield I was writing data with the keyboard. I thought this was default.
I added a lot of code in the uiview and added a button linking to another view. Somehow in the middle of that process my uiview lost its "auto-scroll-when-next-is-pressed" mechanism. I tried deleting my button and messed around a bit but couldn't find the problem.
Not sure if I should post the whole class.
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[textField resignFirstResponder];
if (textField == self.firstOne) {
[self.secondOne becomeFirstResponder];
} else if (textField == self.secondOne) {
[self.thirdOne becomeFirstResponder];
} else if (textField == self.thirdOne) {
[self.fourthOne becomeFirstResponder];
} else if (textField == self.fourthOne) {
[self.fifthOne becomeFirstResponder];
}
return NO;
}
I also found some content on how to scroll screen programmatic by setting up coordinates. Not very viable in my opinion. I want it to be the way it was "Knowing where to scroll down automatically".
I found out that the reason is I added viewWillAppear method when I introduced the new button. That code didn't call the super method, so by just adding 1 line of code the problem was fixed
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// your code
}
I hope that add the ScrollView in View and next step add the 5 TextField in SubView of the UIScrollView such like as
[scrollView addSubView:textField1];
Next increase and decrease the scrollview content of set. Such as
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[scrollView setContentOffset:CGPointMake(0, 230) animated:YES];
}
Related
I have a lot of textfields arranged vertically in a scrollview and at the very bottom one textview. All are wired in storyboard to outlets in a .h file and also to action methods that trigger a method to enable a save button. All have their delegates set in viewdid load. However, the bottom textfields--those below the bottom of the screen when it appears onscreen are not responding to touches.
The textfields visible when you open screen respond to touches. The fields below do not. This is baffling. I am using autolayout but have tried all sorts of settings for the sizes of the view, contentview and scrollview without success. Right now I have them all the same size (667 height).
Using the visual debugger I do not see any views blocking the textfields. I do see the scrollview being tapped. Since the scrollview has a gesture recognizer, I have put in code to screen against touches to the textfields with no luck.
Can anyone suggest what might be wrong?
Here is some code that might be relevant:
//in view will appear
//for dismissing keynoard from scroll
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapped)];
tapScroll.cancelsTouchesInView = TRUE;//was no
[self.scrollView addGestureRecognizer:tapScroll];
- (void) tapped
{
NSLog(#"tapped fired");
[self.view endEditing:YES];
}
//setting delegates
self.titleField.delegate=self;
self.firstField.delegate=self;
self.lastField.delegate = self;
self.emailField.delegate=self;
self.email2Field.delegate=self;
self.telField.delegate=self;
self.mobField.delegate=self;
self.telmainField.delegate=self;
self.teltollField.delegate=self;
self.telhField.delegate=self;
self.telfaxField.delegate=self;
self.jobtitleField.delegate = self;
self.coField.delegate = self;
self.addr1Field.delegate = self;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
NSLog(#"gesture recognizer should receive touch");
// Disallow recognition of tap gestures in the segmented control.
if ((touch.view == _titleField)||(touch.view == _firstField)||(touch.view == _lastField)||(touch.view == _emailField)||(touch.view == _email2Field)
||(touch.view == _telField)||(touch.view == _mobField)||(touch.view == _telmainField)||(touch.view == _teltollField)||(touch.view == _telhField)||(touch.view == _telfaxField)||(touch.view == _jobtitleField)||(touch.view == _coField)||(touch.view == _addr1Field)) {//change it to your condition
return NO;
}
else {
return YES;
}
}
- (void)viewDidLayoutSubviews {
self.scrollView.contentSize = CGSizeMake(320, 1200);
}
My problem is that I want to make a UITextField like WhatsApp verification code textField(like below image).Where default value contains number of character to be entered(as hint for user). When user enter text replace hint character with entered character one by one?
Any help is much appreciated.
Please refer below link of my previous answer. Hope you will get an idea how to do this. Only single UITextField, No third party library use.
link- https://stackoverflow.com/a/36769911/5097148
for don't use of paste make couple of changes-
add myLable above textField then add this code in viewDidLoad()
self.myLable.userInteractionEnabled=true;
UITapGestureRecognizer *LongPressgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapPressgesture:)];
tapPressgesture.delegate=self;
[self.myLable addGestureRecognizer:tapPressgesture];
and
- (void)tapPressgesture:(UITapPressGestureRecognizer *)recognizer
{
[self.txtField becomeFirstResponder];
}
for background like your image you can set textField background image property or add bottom border to your textfield. UITextField border style must be UITextBorderStyleNone If you have any issue then let me know.
Happy coding:)
After adding 6 Textfields and background image.
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
if (textField==textfield1)
{
[textfield1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
else if (textField==textfield2)
{
[textfield2 resignFirstResponder];
[textField3 becomeFirstResponder];
}
}
continue till textfield 6
I'm using the following to raise a textfield inside a table cell above keyboard level. But this only works if I tap the textfield. If I focus the text field programmatically ,i.e., [textField becomeFirestResponder], it doesn't work. Edit: also textFieldShouldBeginEditing: is being called but the code inside isn't performing as expected.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:_tableView];
CGPoint contentOffset = _tableView.contentOffset;
contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);
[_tableView setContentOffset:contentOffset animated:YES];
}
return YES;
}
Try calling the [self textFieldShouldBeginEditing:textField] after setting [textField becomeFirstResponder]
call [textField becomeFirstResponder] and implement your keyboard avoiding code in textFieldDidBeginEditing as it is called after editing has begun. On the other hand textFieldShouldBeginEditing is called before beginning editing. You can properly obtain the keyboard height after editing has begun.
You can use third party class TPKeyboardAvoidingTableView
Which will automatically adjust and scroll TableView when keyboard get opens.
https://github.com/michaeltyson/TPKeyboardAvoiding
The task is to move the textfield up when user done editing.
Right now, I have a text field textfield in the centre of the view, and a method moveTextFieldToTheTop.
Here is the code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[_textField resignFirstResponder];
return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self moveTextFieldToTheTop];
}
- (void)moveTextFieldToTheTop
{
[UIView animateWithDuration:0.3
animations:^{
_textField.frame = CGRectOffset(_textField.frame, 0, -100);
}];
}
Instead of moving 100px up from the centre, textfield would somehow appear 100px below the centre and move to the centre position.
I debugged the code, and find out
[_textField resignFirstResponder];; is the cause of this problem.
But I really can't find out why and how to solve this problem.
Can someone please help me?
Update
I was using auto layout when I came across this problem. If I uncheck use auto layout, problem solved.
But is there a way to solve this problem with auto layout checked?
With auto layout change your moveTextFieldToTheTop method with this:
- (void)moveTextFieldToTheTop
{
//TODO update _textField's constraint to move _textField 100pt up
[_textField setNeedsLayout];
[UIView animateWithDuration:0.3
animations:^{
[_textField layoutIfNeeded];
}];
}
if you are using storyboard, then connect constraint of _textField to view controller, and update the code TODO part with that constraint(you need to change constraint .const property by 100)
Update
Here is the example project with textfield, just run the example, touch on textfield to open keyboard, and then press return button on keyboard.
https://www.dropbox.com/s/x138ta2uy1z3bbo/ToolBarhighlightproblem.zip?dl=0
I am using a subclass of a scrollview that moves the keyboard out of the way. (TPKeyboardAvoidingScrollView)
I think this is conflicting with my implementation of the next and previous buttons. I have built an inputaccessoryview
I have a category that sets the next and previous textfields for each field
when i edit a textfield, i set the current, previous and next textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
_currentTextField = textField;
_prevTextField = [textField prevTextField];
_nextTextField = [textField nextTextField];
return YES;
}
when they click next or previous i call this method
- (void)selectAdjacentResponder:(id)sender{
UISegmentedControl *segmented = sender;
if(segmented.selectedSegmentIndex == 0){
[_prevTextField becomeFirstResponder];
} else {
[_nextTextField becomeFirstResponder];
}
}
this works fine.. but when i close the keyboard. my scrollview is messed up. if i add the line
[_currentTextField resignFirstResponder];
to the first line of my selectadjacent method it solves the problem. but the problem is it makes the screen focus in a funky way since i'm dismissing and accessing the next textfield at the same time.
i have tried resigning first responder when i close the keyboard. but i think my scrollview is calculated before that point.. any idea what to do =/