I have a view that contains 10 UITextFields created programatically. I want the following behavior:
When I click on a particular UITextField, the keyboard should hide all the text fields which are visually below the selected text field.
If I have a text field selected and change the device orientation, the text field and the keyboard should both rotate to the proper orientation without the text field losing the selection focus.
I need to control whether the return key in the keyboard is active.
How do I manage these text fields to get these behaviors.
Add your textfield in scrollview and set tag for all the textfield.Than put following code in you application.You must have to enter you textfield position as per your requirements.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Begin animations to move TextFields into view.
if (textField.tag == 1) {
[UIView beginAnimations: #"moveField" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.scrlview.frame = CGRectMake(0,30,320,357);
[UIView commitAnimations];
textfield2.hidden=YES;
textfield3.hidden=YES;
textfield4.hidden=YES;
}
else if(textField.tag == 2)
{
[UIView beginAnimations: #"moveField" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.scrlview.frame = CGRectMake(0,30,320,357);
[UIView commitAnimations];
textfield1.hidden=YES;
textfield3.hidden=YES;
textfield4.hidden=YES;
}
else if(textField.tag == 3)
{
[UIView beginAnimations: #"moveField" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.scrlview.frame = CGRectMake(0,25,320,357);
[UIView commitAnimations];
textfield1.hidden=YES;
textfield2.hidden=YES;
textfield4.hidden=YES;
}
else if(textField.tag == 4)
{
[UIView beginAnimations: #"moveField" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.scrlview.frame = CGRectMake(0,20,320,357);
[UIView commitAnimations];
textfield1.hidden=YES;
textfield2.hidden=YES;
textfield3.hidden=YES;
}
return YES;
}
//Set the objects on the view when device orientation will change.
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==UIInterfaceOrientationLandscapeRight) {
// set the views oreintation here for landscapemode.
}
if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ||
interfaceOrientation == UIInterfaceOrientationPortrait) {
//set the views oreintation here for Portraitmode.
}
}
//Delegate called when orientation will change
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
Related
I have a UIScrollView that contains a text field and a text view. I have code to move the text fields up when the keyboard is present so the keyboard does not cover the text fields. This code works great in portrait view:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField) {
[textField resignFirstResponder];
}
return NO;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textViewDidBeginEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textViewShouldEndEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
When I rotate the iPhone simulator into a landscape view, this (not surprisingly) does not work. How can I get the text field to move up just enough to see it while typing into it in both landscape and portrait views?
Also, if I rotate from landscape to portrait while the keyboard is shown, after dismissing the keyboard the scroll view has moved down the screen instead of lined up in its original position. How can I avoid this?
Apple's document Managing the Keyboard shows the proper way to do this under the heading "Moving Content That Is Located Under the Keyboard".
You basically put your content on a scroll view and use the UIKeyboardDidShowNotification and UIKeyboardWillHideNotification notifications to adjust the content offsets.
However, the code is somewhat incomplete. The keyboard size calculation doesn't work in landscape mode. To fix it, replace this:
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
With this:
// Works in both portrait and landscape mode
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect toView:nil];
CGSize kbSize = kbRect.size;
I have a UIButton set up in a storyboard that when pressed animates a UIVIew. I would like the same button to animate/move the UIView back to it's original point when it is pressed a second time.
What is the best way to do this?
thanks for any help or pointers.
this is the code I'm using to animate and move the UIVIew:
I'm using a BOOL named buttonCurrentStatus.
-(IBAction)sortDeals:(UIButton*)sender {
if (buttonCurrentStatus == NO)
{
buttonCurrentStatus = YES;
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(#"Sort Deals touched button status = yes");
} else {
buttonCurrentStatus = NO;
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(#"Sort Deals touched button status = no");
}
Another more general way is to use an IBAction that will execute one of two methods. Like previous answers, you can use a BOOl value, or int in order to determine what action to use. This is a more general answer, and can be used for many purposes.
First, you will need a BOOL variable. For this example I put the Bool variable as boolVarible (Yes, I know I spelled it wrong). By default I have it set to YES.
bool boolVarible = YES;
I made that a class variable.
-(IBAction)buttonAction:(id)sender {
if (boolVarible == YES)
{
//Execute first action
[self firstAction];
}
else
{
//Execute second action
[self secondAction];
}
}
-(void)firstAction {
//Do something here...
}
-(void)secondAction {
//Do something here...
}
Hopefully, you get the idea. You can then swap actions whenever you want. Just simply change the BOOl value, and then when the button is pressed, it will execute another method. I find this cleaner than doing it all in one action. Good luck.
-(IBAction)sortDeals:(UIButton*)sender {
sender.selected = !sender.selected;
int moveby = (sender.selected) ? 30: -30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
menuTop.frame = CGRectMake(menuTop.frame.origin.x, menuTop.frame.origin.y + moveby, menuTop.frame.size.width, menuTop.frame.size.height);
[UIView commitAnimations];
}
Very Simply Solution for your Question is if you Declare two Boolean Variables in Class where you need to implement this method.your Code will be look like this
In .m file
BOOL Action1;
BOOL Action2;
Now In ViewDidload method
(void)viewDidLoad
{
Action1=TRUE;
Action2=FALSE;
[super viewDidLoad];
}
And now your Method
(IBAction)sortDeals:(UIButton*)sender {
if (Action1 == TRUE)
{
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(#"Sort Deals touched button status = yes");
Action1=FALSE;
Action2=TRUE;
}
else
if (Action2 == TRUE)
{
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(#"Sort Deals touched button status = no");
Action1=TRUE;
Action2=FALSE;
}
Hope it Works for you.Thanks
in my view i have 14 text fields in a scrollview.
To move the view up /down while keyboard appears/ disappears i have set the scroll view frame sizes
#pragma mark - Text field view delegate methods
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
//To move the scroll view up to avoid keybord covers the text field
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
//scrollView.frame = CGRectMake(scrollView.frame.origin.x, (scrollView.frame.origin.y - 45), scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView setContentSize:CGSizeMake(200, 1100)];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
//To move the view down
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
[scrollView setContentSize:CGSizeMake(200, 900)];
//scrollView.frame = CGRectMake(scrollView.frame.origin.x, (scrollView.frame.origin.y + 45), scrollView.frame.size.width, scrollView.frame.size.height);
[UIView commitAnimations];
}
for return key board focussed to the next field and for the last field keyboard resign
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if(textField == nameField) {
[emailText becomeFirstResponder];
} else if(textField == emailText) {
[mobileText becomeFirstResponder];
}
else if(textField == mobileText) {
[iPhoneText becomeFirstResponder];
}
else if(textField == iPhoneText.value) {
[companyText becomeFirstResponder];
}
else if(textField == companyText) {
[roleText becomeFirstResponder];
}
...
......
...........
else if(textField == lastfield) { // Last fiels
[textField resignFirstResponder];
}
return YES;
}
But my intension is when ever a text field is focussed that text field is moving up to the keyboard top or center of the view
How to do it
I use TPKeyboardAvoiding for my apps, it seems to work perfectly for me, perhaps give it a try.
I am havng one textfield and two textViews...while writting something in 1 conrol the keyboard is poped up...but when I want to shift on another control my keyboard doesn't let me write as it covers the whole view...hw can I solve my problem?
you should move your view up, so that the keyboard doesnt cover the textfield/ textview. something like this...
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == *textFieldName*)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 65.0), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == *textFieldName*)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 65.0), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
and for the textView use:
- (void)textViewDidBeginEditing:(UITextView *)textView
and
- (void)textViewDidEndEditing:(UITextView *)textView
Use delegate functions provided for uitextview and uitextfiled ....
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 150; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[self animateTextField: textField up: YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView{
[self animateTextField: textField up: NO];
}
you can shift your whole view up or just shift the controls (text field and text view) up .. or make your view scrollable so the user can scroll down while the keyboard is visible.
I'm trying to create a search bar similar to the one in safari browser on iPad. I think its a UITextview only.
On click of the control, its size will expand. And when orientation is rotated, it maintains the size accordingly.
How can I achieve that using auto-resizing option? Or do I've to code manually to achieve it?
You can do all this directly in Interface Builder.
The Search Bar component gives you the appropriate functionality. To make the bar resize properly, you just need to anchor it to the appropriate sides of the screen and make it stretchable. Try opening this file with IB for an example.
Use the below code to your controller and make sure the you have a textfield delegate.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UIInterfaceOrientation orientation = self.interfaceOrientation;
if (orientation== UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
if(textField==searchField){
CGRect searchFrame = searchField.frame;
searchFrame.size.width += 150;
searchFrame.origin.x -= 150;
[UIView beginAnimations: #"GrowTextField" context: nil];
{
searchField.frame = searchFrame;
[UIView setAnimationDuration: 0.5];
}
[UIView commitAnimations];
}
}
else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
if(textField==searchField){
CGRect searchFrame = searchField.frame;
searchFrame.size.width += 150;
searchFrame.origin.x -= 150;
[UIView beginAnimations: #"GrowTextField" context: nil];
{
searchField.frame = searchFrame;
[UIView setAnimationDuration: 0.5];
}
[UIView commitAnimations];
}
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
UIInterfaceOrientation orientation = self.interfaceOrientation;
if (orientation== UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
if(textField==searchField){
CGRect searchFrame = searchField.frame;
searchFrame.size.width -= 150;
searchFrame.origin.x += 150;
[UIView beginAnimations: #"ShrinkTextField" context: nil];
{
searchField.frame = searchFrame;
[UIView setAnimationDuration: 0.5];
}
[UIView commitAnimations];
}
}
else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
if(textField==searchField){
CGRect searchFrame = searchField.frame;
searchFrame.size.width -= 150;
searchFrame.origin.x += 150;
[UIView beginAnimations: #"ShrinkTextField" context: nil];
{
searchField.frame = searchFrame;
[UIView setAnimationDuration: 0.5];
}
[UIView commitAnimations];
}
}
}