I have many text fields arranged vertically on the iPhone screen. Each field call the keyboard.
The problem is with the fields at the bottom of the screen, because the keyboard pops up and covers them, so the user can not see them.
I've added UIScrollView and drag all the text fields on it - but the scrolling does't work.
Should I add some code?
How do I implement the scroll view?
add a delegate for all the textfields and use the below code as per your requirement
here self.scrMain is my scrollview object
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == txtRegNo) {
[self.scrMain setContentOffset:CGPointMake(0, 20) animated:YES];
}
if (textField == txtName) {
[self.scrMain setContentOffset:CGPointMake(0, 80) animated:YES];
}
if (textField == txtMobile) {
[self.scrMain setContentOffset:CGPointMake(0, 150) animated:YES];
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.scrMain setContentOffset:CGPointMake(0, 0) animated:YES];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.scrMain setContentOffset:CGPointMake(0, 0) animated:YES];
[textField resignFirstResponder];
}
Related
How to resign a UITextField of UISearchBar? I tried with the below code
UIView * subView;
NSArray * subViews = [self.searchBar subviews];
for(subView in subViews)
{
if( [subView isKindOfClass:[UITextField class]] )
{
((UITextField*)subView).delegate=self;
((UITextField*)subView).returnKeyType=UIReturnKeyDone;
break;
}
}
for (UIView *subview in self.searchBar.subviews)
{
if ([subview conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}
And added the TextField Delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
this works but this resigns the keyboard oly if the textfield is not empty. If the textfield is empty then it is not resigning
Use this :
[searchBar resignFirstResponder];
Please do this one...
-(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
if (searchBar.text.length == 0)
[searchBar resignFirstResponder];
}
i hope this will work fine.
First set Delegate in .h file like bellow...
#interface yourViewController : UIViewController<UISearchBarDelegate>
and when you want to resign keyboard of UISearchBar simple use bellow line..
[searchBar resignFirstResponder];
UPDATE:
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
[self.searchBar resignFirstResponder];
return YES;
}
I'm trying to hide the keyboard when the user selects a UITextField. I currently have three text fields: two UIPickerView (controlled by the same picker) and a textinput field. When I click "return" or on the background, the keyboard disappears via a resignFirstResponder call.
PROBLEM:
When I am currently editing the text input field and then immediately select the UITextField w/ UIPickerView functionality, the keyboard doesn't disappear. I feel like i've tried every solution and am beating by head against a wall...
CODE:
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[pickerView setHidden:YES];
if (fldQuiver.editing == YES) {
[fldTitle resignFirstResponder];
[fldQuiver resignFirstResponder];
[pickerView setHidden:NO];
variabla = 1;
}else if (fldCategory.editing == YES) {
[fldTitle resignFirstResponder];
[fldCategory resignFirstResponder];
[pickerView setHidden:NO];
variabla = 2;
}
NSLog(#"variabla %d",variabla);
[pickerView reloadAllComponents];
}
Any help would be appreciated. Thanks in advance.
EDITED CODE FOR PRINCE:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
{
//set logic for picker view here
if (textField == fldQuiver)
{
variabla = 1;
}
else if (textField == fldCategory) {
variabla = 2;
}
else
{
}
NSLog(#"variabla %d",variabla);
[pickerView reloadAllComponents];
if (textField == fldQuiver)
{
[fldTitle resignFirstResponder];
[pickerView setHidden:NO];
return NO;
}
else if (textField == fldCategory) {
[fldTitle resignFirstResponder];
[pickerView setHidden:NO];
return NO;
}
else
{
[pickerView setHidden:YES];
return YES;
}
}
ALSO: I have "synthesized" and declared #property fldQuiver and fldCategory. I have IBOutlet for fldTitle, fldQuiver, fldCategory.
The pickerView is loaded with an array based on variable.
Use textFieldShouldBeginEditing delegate method for this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
{
//set logic for picker view here
if (textField == fldQuiver)
{
//picker view hidden or show here
return NO;
}
else if (textField == fldCategory) {
//picker view hidden or show here
return NO;
}
else
{
return YES;
}
}
set delegate for text field
textField.delegate=self;
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
...........
[textField resignFirstResponder]; //it common for all text field,so not use multi resignFirstResponder
............
}
I have a UIScrollView in which inside of that I have a UITextField, so what I did is that on the third text field, which is password, I scrolled down the offset of the UIScrollView:
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ((textField == self.emailTextField_ || textField == self.passwordTextField_)){
if (self.scrollView_.contentOffset.y != self.emailTextField_.frameY - self.emailTextField_.frameHeight/2){
shouldAdjustOffset = YES;
} else {
shouldAdjustOffset = NO;
}
if (shouldAdjustOffset){
[self.scrollView_ setContentOffset:CGPointMake(0, self.emailTextField_.frameY - self.emailTextField_.frameHeight/2) animated:YES];
}
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.firstNameTexField_){
[self.firstNameTexField_ resignFirstResponder];
[self.lastNameTextField_ becomeFirstResponder];
} else if (textField == self.lastNameTextField_){
[self.lastNameTextField_ resignFirstResponder];
[self.emailTextField_ becomeFirstResponder];
} else if (textField == self.emailTextField_){
[self.emailTextField_ resignFirstResponder];
[self.passwordTextField_ becomeFirstResponder];
} else if (textField == self.passwordTextField_){
[self signupButtonPressed:nil];
[self.passwordTextField_ resignFirstResponder];
}
return YES;
}
Now the issue is that when I am on the next text field it resets the content offset back to 0, when I do:
[self.emailTextField_ resignFirstResponder];
[self.passwordTextField_ becomeFirstResponder];
. How can I prevent this from happening?
Just turn of scrollview scroll before showing keypad (becomeFirstResponder) and enable back the scroll after it,
scrollView.scrollEnabled = NO;
[someControl becomeFirstResponder];
scrollView.scrollEnabled = YES;
[scrollview doTheScrollingHere];
#Deepan you saved me a lot of time :) Only what I'll suggest is to override UITextField method becomeFirstResponder to check first if it is in UIScrollView and if is disable scroll for a moment
- (BOOL)becomeFirstResponder
{
if ([self.superview isKindOfClass:[UIScrollView class]])
{
[(UIScrollView*)self.superview setScrollEnabled:NO];
}
BOOL accept = [super becomeFirstResponder];
if ([self.superview isKindOfClass:[UIScrollView class]])
{
[(UIScrollView*)self.superview setScrollEnabled:YES];
}
return accept
}
i have 6 text field.i want to get current cursor position(cursor in which text field).like android onfocus .after i get the position i need to draw slider near to that textfield. is there any API for iPhone.guide me i'm new to iPhone and Xcode.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
Add your own logic for ‘for' loop here. I am using my logic. (As per my requirements).
NSArray *subviews = [[NSArray alloc] initWithArray:self.view.subviews];
for(UIView *subview in subviews)
if([subview isKindOfClass:[UITextField class]])
{
UITextField *textField = (UITextField *) subview;
if([textField isFirstResponder])
{
// do whatever you want
}
}
If You want to see TextField In center OF Screen hen Use
Firstly You Add ScrollView in nid and connect and all TextField's delegete is connect with fileOwner
and Implement this code
It"s Better according to your View
#pragma mark -
#pragma mark textField Delegte
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self scrollViewToCenterOfScreen:textField];
return YES;
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard
CGFloat y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
if ([textField isEqual:textField1])
{
[textField2 becomeFirstResponder];
}
else if ([textField isEqual:textField2])
{
[textField3 becomeFirstResponder];
}
else if ([textField isEqual:textField3])
{
[textField4 becomeFirstResponder];
}
else
{
[textField4 resignFirstResponder];
[scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
}
return YES;
}
How do I use the next button (the done button) in the bottom left of the keyboard on the iPhone?
if i understand the question.
Here is solution
Code snippit from link:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == field1TextField) {
[textField resignFirstResponder];
[field2TextField becomeFirstResponder];
}
else if (textField == field2TextField) {
[textField resignFirstResponder];
[field3TextField becomeFirstResponder];
}
else if (textField == field3TextField) {
[textField resignFirstResponder];
}
return YES;
}