Help me on UITextField - iphone

i have list of UITextFields.
i want to move to the next text fields one by one after entering input in it.
since the keyboard is hiding the fields....
can you people show me that how can i accomplish that...
Thank a lot for your time.

(void)shiftViewUp:(BOOL)up
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if (movedUp)
{
if(rect.origin.y == 0){
rect.origin.y -= 85.0;
rect.size.height += 85.0;
}
}
else
{
if(rect.origin.y != 0.0){
rect.origin.y += 85.0;
rect.size.height -= 85.0;
}
}
self.view.frame = rect;
[UIView commitAnimations];
}
Basically you can call this method with BOOL YES when you begin editing the textfiled & with BOOL no when you end editing.
You just need to track which textfield is editing based on that you can adjust shifting (eg 85 here)

An alternative approach could be to use a UITableView. Then most of the controls are ready. But you can also make it all happen by yourself.
First put your UITextfields on a separate UIView. Make that UIView an outlet en link it properly. That way you can easily move the textfields up with an animation.
Then do something like this in the delegate methods:
// Sets the label of the keyboard's return key to 'Done' when the insertion
// point moves to the table view's last field.
//
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField tag] == myLastUITextField)
{
[textField setReturnKeyType:UIReturnKeyDone];
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField returnKeyType] != UIReturnKeyDone)
{
// If this is not the last field (in which case the keyboard's
// return key label will currently be 'Next' rather than 'Done'),
// just move the insertion point to the next field.
//
NSInteger nextTag = [textField tag] + 1;
UIView *nextTextField = [myTextView viewWithTag:nextTag];
// here starts your animation code
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = textFieldView.frame;
rect.origin.y = -44;
textFieldView.frame = rect;
[UIView commitAnimations];
// here ends your animation code. Play with it
[nextTextField becomeFirstResponder];
}
else
{
// do what you want to do with the last UITextfield
}
return YES;
}
`
I think it will not immediately work by copy-pasting but I hope it points in a direction. Good luck

Related

Moving Text Fields during input

I have set up some text fields and labels to look like a simple form which will have to get some user input. My problem is however that whenever some of the text fields are selected and the keyboard slides out, the text fields are covered making it impossible to see the input. My question is how can I move up the text fields so that they stay in view when selected. Maybe someone who is more experienced can help me on this.
A good solution for this which I like to do is listen to when the textfield begins editing with the following delegate function:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//keep a member variable to store where the textField started
_yPositionStore = textField.frame.origin.y;
//If we begin editing on the text field we need to move it up to make sure we can still
//see it when the keyboard is visible.
//
//I am adding an animation to make this look better
[UIView beginAnimations:#"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
160 , //this is just a number to put it above the keyboard
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
and then in this callback you can return it to its original location:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:#"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
_yPositionStore ,
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
}
You will need to declare _yPositionStore as a member variable of CGFloat and have your textFields delegate be set to the view controller that owns it(either using Interface builder and dragging delegate to files owner or setting yourTextField.delegate = self)
I hope that helps
Do do this you need to follow next 2 steps:
- put all your UITextViews into UIScrollView
- register for keyboard notifications and slide your UIScrollView when keyboard appears
Both steps explained here: "Moving Content That Is Located Under the Keyboard"
You can call delegate methods of UITextfield and customize it according to requirement.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 110;
const float movementDuration = 0.3f;
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: #"aimation" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
yourView.frame= CGRectOffset(yourView.frame,0, movement);
[UIView commitAnimations];
}
For my applications I use the following code assuming that keyboardHeight is 216 for portrait and 162 for landscape mode:
#pragma mark - textField delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// keyboard is visible, move views
CGRect myScreenRect = [[UIScreen mainScreen] bounds];
int keyboardHeight = 216;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
int needToMove = 0;
CGRect frame = self.view.frame;
if (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height > (myScreenRect.size.height - keyboardHeight)) {
needToMove = (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height) - (myScreenRect.size.height - keyboardHeight);
}
frame.origin.y = -needToMove;
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// resign first responder, hide keyboard, move views
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
CGRect frame = self.view.frame;
frame.origin.y = 0;
[self.view setFrame:frame];
[UIView commitAnimations];
}
This formula takes into account the status bar, navigation bar and display size. And of course do not forget to set the delegate for your fields.
And with:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
you can hide the keyboard, when the user tap outside the field.

I have a UITextField that gets hidden by the keyboard when selected. How can it be brought into view?

I have a UITableView that is shorter than the window, therefore it does not need to scroll. However, it is long enough that when a text field in the bottom row is selected, the keyboard covers it.
I can't use scrollToRowAtIndexPath because the table is shorter than the window, so I was wondering what the correct way to bring it into view would be.
I was thinking about sliding the whole view up a set number of pixels, although that seems very bad form because it would break the UI if I added more rows to the table.
You should implement these methods in the concerned class :
- (void) textFieldDidBeginEditing:(UITextField *)myTextField
{
[self animateTextField:myTextField up:YES];
}
- (void) textFieldDidEndEditing:(UITextField *)myTextField
{
[self animateTextField:myTextField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int movement = (up ? -105 : 105);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
You have to adapt values (-105, 105 and 0.3f) to your situation.
You can have the whole tableView slide up by setting the height of the footerView. The Keyboard will move the table above for the height of the footer
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 70.0;
}
Pierre's code worked for me, but I removed the myTextField argument. It seemed unnecessary. I also changed this over to UITextViews because that's what I had in my code. Otherwise, thanks for the question and answers. I've been beating my head against the wall to solve this problem!
#pragma mark - Text View Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self animateTextViewUp:YES];
}
- (void) textViewDidEndEditing:(UITextView *)textView
{
[self animateTextViewUp:NO];
}
- (void) animateTextViewUp:(BOOL)up
{
int movement = (up ? -80 :80);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}

resize UIView when showing the keyboard for iphone, how to? [duplicate]

This question already has answers here:
How to resize UITextView on iOS when a keyboard appears?
(10 answers)
Closed 9 years ago.
I will show you an example with the well known whatsapp
When you touch inside the text the keyboard pops up , so I have to move or shift all that bar up and resize the view to half, so I can still see the text that I'm typing and the send button
Phase 1:
http://www.appbank.net/wp-content/uploads/2010/10/WhatsAppMessenger-18.jpg
Phase 2:
http://www.onetooneglobal.com/wp-content/uploads/2011/02/onetoone_whatsapp_2.png
What would be the best way to achieve this?
#define kOFFSET_FOR_KEYBOARD 280.0
- (void)keyboardWillHide:(NSNotification *)notif {
[self setViewMoveUp:NO];
}
- (void)keyboardWillShow:(NSNotification *)notif{
[self setViewMoveUp:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
stayup = YES;
[self setViewMoveUp:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
stayup = NO;
[self setViewMoveUp:NO];
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMoveUp:(BOOL)moveUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
CGRect rect = self.view.frame;
if (moveUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
if (rect.origin.y == 0 ) {
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
//rect.size.height += kOFFSET_FOR_KEYBOARD;
}
}
else
{
if (stayup == NO) {
rect.origin.y += kOFFSET_FOR_KEYBOARD;
//rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
}
self.view.frame = rect;
[UIView commitAnimations];
}
Try this methods. Edit it according to your requirement.
You'll want to listen to the UIKeyboardDidShowNotification and UIKeyboardDidHideNotification, and in the method that corresponds to the selector you've provided the notification center resize your views at will (typically by changing the UIView.frame property)

How to create a collapsible UIView

I need to create a expandable and collapsible UIView of some sort and am not able to pay for a third party control.
This is basically how I would like it to behave:
At the top there should be a UIButton (or similar) that allows the user to toggle between expanded and collapsed.
When expanded I want to be able to place other UIView (e.g. calendar)and when collapsed the surrounding controls should move up.
Does anyone have any ideas how to implement this simply - noob here :(
Subclass a ViewController and have two methods that your button can fire like 'collapse' and 'expand', this might get you started: You can assign new selectors to UIButtons dynamically:
[button addTarget:self action:#selector(eventMethod:)
forControlEvents:UIControlEventTouchUpInside];
Code:
#define COLAPSED_HEIGHT 30
-(void)expand
{
CGRect s= [self getScrerenBoundsForCurrentOriantation];
CGRect f = self.view.frame;
f.origin.y =s.size.height-self.view.frame.size.height;
[UIView beginAnimations:#"expand" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
self.view.frame=f;
//CHANGE YOUR EXPAND/COLLAPSE BUTTON HERE
[UIView commitAnimations];
self.thumbScrollerIsExpanded=YES;
}
-(void)collapseView
{
//re-factored so that this method can be called in ViewdidLoad
CGRect s= [self getScrerenBoundsForCurrentOriantation];
CGRect f = self.view.frame;
f.origin.y =(s.size.height-COLAPSED_HEIGHT);
self.view.frame = f;
self.thumbScrollerIsExpanded=NO; //thumbScrollerIsExpanded is a BOOL property
}
- (void)collapse
{
[UIView beginAnimations:#"collapse" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
[self collapseView];
//CHANGE YOUR EXPAND/COLLAPSE BUTTON HERE
[UIView commitAnimations];
}
-(CGRect)getScrerenBoundsForCurrentOriantation
{
return [self getScrerenBoundsForOriantation:[[UIDevice currentDevice] orientation]];
}
-(CGRect)getScrerenBoundsForOriantation:(UIInterfaceOrientation)_orientation
{
UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds; // always implicitly in Portrait orientation.
if (UIInterfaceOrientationIsLandscape(_orientation))
{
CGRect temp;
temp.size.width = fullScreenRect.size.height;
temp.size.height = fullScreenRect.size.width;
fullScreenRect = temp;
}
return fullScreenRect;
}
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
if ([animationID isEqualToString:#"expand"])
{
//example
}
else if ([animationID isEqualToString:#"collapse"])
{
//example
}
}
You could try adding a UIView (either in xib or programmatically) off screen, then use a UIView animation to slide it into your main viewing area.
The button would have an IBAction which would toggle the animation (slide in/slide out). You can slide it off screen then set UIView's hidden property to true. When you recall the IBAction, have it check if the view's hidden property is true. If it is, you know to play the animation of it sliding in, if not, then you play the animation of sliding out.

scroll to a specific textfield in a scrollview/scrollview stop scrolling when keyboard is on screen

I got stuck in here,
I have a custom scrollview in a view this scroll view has an add field button which lets user to add new text field to the scrollview. When user taps on a particular textfield keyboard appears and hide the textfield, to overcome this I followed UICatalog example, but it moves the whole scrollview up
to prevent this I followed UICatalog example and did this
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if (sender.frame.origin.y>109) {
moveScrollViewUpBy=(sender.frame.origin.y-109+10);
[self viewMovedUp:YES];
}
}
-(void)viewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = formScrollView.frame;
if (movedUp)
{
if(rect.origin.y == 0.0f){
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
}
else
{
if(rect.origin.y != 0.0f){
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
}
self.formScrollView = rect;
[UIView commitAnimations];
}
here
#define kOFFSET_FOR_KEYBOARD 160.0
but this shifts the scroll view up,
I want that when i tap on a textfield down below in scroll view... scroll view scrolls and that textfield appears....
is it possible to do so....otherwise suggest me some other solution
I have one more query srollview just stop scrolling while the keyboard stays on screen. Is there any way to overcome this
You probably just want to set the scrollView's contentOffset property, rather than adjust its bounds.
Implement the UITextViewDelegate and do the following:
- (void)textViewDidBeginEditing:(UITextView *)textView {
keyboardShowing = YES; //helper ivar
[self sizeToOrientation];
[self.scrollView scrollRectToVisible:textView.frame animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
keyboardShowing = NO;
[self sizeToOrientation];
}
Helper method:
//fit to the appropriate view sizes
- (void)sizeToOrientation {
CGSize size;
if( keyboardShowing )
size = CGSizeMake(320, 190); //make room for keyboard
else
size = CGSizeMake(320, 420); //full height with tabbar on bottom
self.scrollView.frame = CGRectMake(0, 0, size.width, size.height);
}
This works well for me.