move UIScrollView up when UITextField is selected - ios

subview assigned to the UIScrollView. when the UITextField is selected UIScrollView should move up. I tried with below code but its not working.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if( textField == citytextfield)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
//set Y according to keyBoard height
[_scrolling setFrame:CGRectMake(0.0,-220.0,320.0,460.0)];
[UIView commitAnimations];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[_scrolling setFrame:CGRectMake(0.0,0.0,320.0,460.0)];
[UIView commitAnimations];
}

Use setContentOffset method of UIScrollView to make it scroll up, yo will need to change the value 50 to your desired value.
[_scrolling setContentOffset:CGPointMake(0,50) animated:YES];

download the tpkeyboardavoidingscrollview.
give your scrollview's class name to tpkeyboardavoidingscrollview.
#property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *scrollView;
set the property and give the connection to scrollview.
In your viewcontroller.h
#import "TPKeyboardAvoidingScrollView"
then check it.

Related

Animate UILabel width with auto layout

I'm trying to animate UILabel's width. For some reason animation doesn't work and label changes it's size immediately. I use autolayout. Here is the code from the sample project I wrote to reproduce this. Tap on the button changes trailing constrains for both the button and the label.
#interface ViewController ()
#property (assign, nonatomic) BOOL controlsResized;
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonTrailingConstraint;
#property (weak, nonatomic) IBOutlet MyLabel *label;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelTrailingConstraint;
- (IBAction)doTapButton:(id)sender;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.label setTranslatesAutoresizingMaskIntoConstraints:NO];
self.label3.text = #"asdfasdfds sklfjfjls sdlkfj jjjjkfjkfdsjkdfsjklffsjkfdsjkl";
}
- (IBAction)doTapButton:(id)sender
{
if (!self.controlsResized)
{
[UIView animateWithDuration:0.5 animations:^{
self.buttonTrailingConstraint.constant = 0;
[self.button layoutIfNeeded];
self.labelTrailingConstraint.constant = 0;
[self.label layoutIfNeeded];
self.controlsResized = YES;
}];
}
else
{
[UIView animateWithDuration:0.5 animations:^{
self.buttonTrailingConstraint.constant = 100;
[self.button layoutIfNeeded];
self.labelTrailingConstraint.constant = 100;
[self.label layoutIfNeeded];
self.controlsResized = NO;
}];
}
}
#implementation MyLabel
- (void)drawTextInRect:(CGRect)rect
{
NSLog(#"drawTextInRect");
[super drawTextInRect:rect];
}
#end
As you can see on the video, it works for the button, but doesn't work for the label.
I tried to investigate and subclassed label from MyLabel class. It appears, that - (void)drawTextInRect:(CGRect)rect gets called immediately on the button tap, as a reason of [self.label layoutIfNeeded]; in the animation block. Why is it so? I would've expect the frame to be changed animated, as it is set inside the animation block. And it works as expected for the UIButton.
Why UILabel doesn't resize animated? Is there a way to fix it?
EDIT:
I tried the approaches suggested in current answers, but they don't work either. I think the animation code is not a problem here, because it works for the button in my example. The problem seems to be related to UILabel specific. UILabel seems to handle animation of size changes differently than other UIView subclasses, but I can't understand why.
I would post a comment but I do not have the necessary reputation for that. Here is what I would do:
self.buttonTrailingConstraint.constant = x;
self.labelTrailingConstraint.constant = x;
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
When you update a constraint you need to take into account that the respective view's constraint you are modifying is not the only one that needs updating, so a layout method call is better made on the parent view.
This worked for me everytime. Hope this helps.
You must set constants of constraints before animate.
Try this :
- (IBAction)doTapButton:(id)sender
{
if (!self.controlsResized)
{
self.buttonTrailingConstraint.constant = 0;
self.labelTrailingConstraint.constant = 0;
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.controlsResized = YES;
}];
}
else
{
self.buttonTrailingConstraint.constant = 100;
self.labelTrailingConstraint.constant = 100;
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.controlsResized = NO;
}];
}
}
There are a couple parts to making this 100% work as expected:
Calling layoutIfNeeded first
Setting the constraint constant outside of the animation block
Calling layoutIfNeeded inside the block
Copied from a working implementation:
[self.view layoutIfNeeded];
self.topContainerConstraint.constant = constraintValue;
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished){ }];
I'd change your function to something like the following (obviously untested):
- (IBAction)doTapButton:(id)sender
{
if (!self.controlsResized)
{
[self.view layoutIfNeeded];
self.labelTrailingConstraint.constant = 0;
self.buttonTrailingConstraint.constant = 0;
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
self.controlsResized = YES;
}];
}
else
{
[self.view layoutIfNeeded];
self.labelTrailingConstraint.constant = 100;
self.buttonTrailingConstraint.constant = 100;
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
self.controlsResized = NO;
}];
}
}

UIView automatically moving by uitextfield keyboard

So i have a really weird problem at my hands and hours of search has provided nothing.
I have a uiview containing a uitextfield. Now initially this view is outside of the visible screen with coordinates like x=-500,y=-500.
However in response to a button press this view animates and moves into the center of the screen.
Now whenever i tap on a uitextfield that is the subview of this view.This view moves back to its original coordinates outside the screen.
I have frantically checked my code and there is nothing that is moving the view outside again once its in. Any help in explaining this very unfamiliar behaviour would be really appreciated.
This code moves the view onto the screen
- (IBAction)Register:(id)sender {
//(self.view.frame.size.width/2)-(self.SignUp_Screen.frame.size.width/2);
//self.login_Screen.hidden = YES;
self.blurView.hidden = NO;
//self.SignUp_Screen.layer.zPosition = 5;
NSLog(#"Register");
self.SignUp_Screen.hidden = NO;
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.SignUp_Screen.frame = CGRectMake(35, 50,self.SignUp_Screen.frame.size.width , self.SignUp_Screen.frame.size.height);
} completion:^(BOOL finished) {
}];
}
and these are the delegate methods for the textfield
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"TextFieldEndEditing");
[textField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(#"textFieldShouldReturn");
[textField resignFirstResponder];
return YES;
}
As Wezly hints at, if you are using autolayout, you don't modify the frame directly anymore. That's the old world. You want to have an Outlet / property for the constraint and animate it.
[UIView animateWithDuration:0.25
animations:^{
SignUp_Screen.centerXConstraint.constant = ...;
SignUp_Screen.centerYConstraint.constant = ...;
[SignUp_Screen layoutIfNeeded];
}];
See here and here for more details.
You should not modify frame if you are using auto layout. You should animate view by animating constraint's constant. For example:
NSLayoutConstraint *viewY; //constraint from superview top to view top
viewY.constant = 100;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.3f animations:^{
[self.view layoutIfNeeded];
}];
The way i solved this problem was by linking an IBOutlet to the constraint I wanted to change and then animating it's constant value.
.h
#interface ViewController : UIViewController {
IBOutlet NSLayoutConstraint *constraintHandle;
}
.m
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
constraintHandle.constant = self.view.center.x;
[SignUp_Screen layoutIfNeeded];
} completion:^(BOOL finished) {
}];
Don't forget to link the IBOutlet to your constraint in your storyboard or xib.

How to Show and Hide UIPickerView like iOS Keyboard?

I have this code to hide UIPickerView by default:
- (void)viewDidLoad
{
[super viewDidLoad];
[_memberList setAlpha:0];
}
and this code to show UIPickerView when a button tapped :
- (IBAction)buttonChooseMember {
[UIView animateWithDuration:0.6 delay:0. options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_memberList setAlpha:1];
} completion:nil];
}
and the last thing is this, to hide keyboard when user tap anywhere :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]]) {
[txt resignFirstResponder];
}else if ([txt isKindOfClass:[UIPickerView class]]) {
[UIView animateWithDuration:0.6 delay:0. options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_registerMLMList setAlpha:0];
} completion:nil];
}
}
}
but all of this just give me 'appear' animation, because it's only changing Alpha value from 0 to 1 (and vice versa). not slide-up or slide-down just like iOS keyboard.
I tried to use this animation below to have iOS keyboard look and feel on my UIPickerView :
- (IBAction)hidePicker {
UIPickerView *pickerView = [[UIPickerView alloc] init]; // default frame is set
float pvHeight = pickerView.frame.size.height;
float y = _screen.bounds.size.height - (pvHeight * -2); // the root view of view controller
[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.picker.frame = CGRectMake(0 , y, pickerView.frame.size.width, pvHeight);
} completion:nil];
}
- (IBAction)showPicker {
UIPickerView *pickerView = [[UIPickerView alloc] init]; // default frame is set
float pvHeight = pickerView.frame.size.height;
float y = _screen.bounds.size.height - (pvHeight); // the root view of view controller
[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.picker.frame = CGRectMake(0 , y, pickerView.frame.size.width, pvHeight);
} completion:nil];
}
I like this animation, it looks like iOS keyboard animation, but the problem with this animation is... when my app is loaded, the UIPickerView is already shows up. how to hide it when it loads up for the first time?
thank you.
All UIResponder objects have an inputView property. The inputView of a UIResponder is the view that will be shown in place of the keyboard when the responder becomes the first responder.
So if you want a UIPickerView to show up instead of the keyboard, you could simply do it by making your UIResponder (like a UITextField) have a UIPickerView as its inputView.
(As a caveat: you probably won't want a bare UIPickerView as the inputView, because you also need to account for when the keyboard would change size, like when you rotate. But this is the general idea.)
In viewDidLoad take one boolean variable and set it's value as TRUE and also set the UIPickerView's frame so that UIPickerView is invisible for first time.Based on the boolean value handle the frame animations to show or hide the picker view.
hidepicker and showpicker method idea is good, and the problem of "UIPicker is visible when the app is loaded" can be overcome by just setting the frame of UIPickerView while initiating it to the position such that it should not be visible...after that you can call the showpicker method to show the picker view.

Hiding the Keyboard on button click?

I am creating iPhone app which I show below.
At the end of screen I have text field. I have added a delegate for the same. As it is number pad, I have added button seperately so that when button is clicked, they keyboard is hidden.
Below is the code I have:
.h
#interface SearchViewController : UIViewController<UITextFieldDelegate>
#property (retain, nonatomic) IBOutlet UITextField *textField006;
#property (retain, nonatomic) IBOutlet UIButton *doneButton;
- (IBAction)doneAction:(id)sender;
.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(#"textFieldShouldReturn");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"textFieldDidBeginEditing");
// Ensure the relevant text field is visible
CGAffineTransform translation = CGAffineTransformIdentity;
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenHeight = screenSize.height;
if (screenHeight==480 || screenHeight==568) {
translation = CGAffineTransformMakeTranslation(0, -120);
doneButton.hidden = NO;
NSLog(#"line 3");
[UIView beginAnimations:nil context:nil];
self.view.transform = translation;
[UIView commitAnimations];
}
}
- (IBAction)doneAction:(id)sender {
doneButton.hidden = NO;
doneButton.hidden = YES;
[textField006 resignFirstResponder];
[UIView beginAnimations:nil context:nil];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
[self.textField006 resignFirstResponder];
}
Why isn't the keyboard hiding? How can I hide it?
Keyboard == Decimal Pad Return key >> Go Auto-enable Return key =
Ticked
Be sure to use endEditing: if it's not currently hiding correctly.
About endEditing:
"endEditing causes the view (or one of its embedded text fields) to resign the
first responder status."
"This method looks at the current view and its subview hierarchy for
the text field that is currently the first responder. If it finds one,
it asks that text field to resign as first responder. If the force
parameter is set to YES, the text field is never even asked; it is
forced to resign."
So, the following should work (inside your button click action method):
[self.view endEditing:YES];
Swift Version of #lifetimes Answer
self.view.endEditing(true)
it worked perfectly for me
You can also make [self.textField006 resignFirstResponder]

UIPickerview can not find the Delegate and DataSource Outlet in Interface Builder

I try to make a UIPickerView show up when the user click on a UITextField, I should see the DataSource and the Delegate Outlets to link them with the picker, however, it doesn't exit when I open the nib file--> click on file owner--> inspector
the second problem is that the keyboard is not hidden when I click on the UItextField although I made a textFieldShouldReturn method which suppose to hide keyboard.
What am I missing here?
the .h file:
#interface RechercherViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate> {
IBOutlet UIPickerView *pickerTypesCarburants;
IBOutlet UIView *pickerViewTypesCarburants;
NSMutableArray *typesCarburantsArray;
IBOutlet UITextField *typeCarburantTextField;
}
-(IBAction)pickerTypeCarburantsShow;
-(IBAction)pickerTypeCarburantsDone;
#end
the .m file :
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [typesCarburantsArray count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component{
return [typesCarburantsArray objectAtIndex:row];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch;
touch=[touches anyObject];
CGPoint point=[touch locationInView:self.view];
if(CGRectContainsPoint([typeCarburantTextField frame],point))
{
[self pickerTypeCarburantsShow];
}
}
-(IBAction)pickerTypeCarburantsDone{
NSInteger selectedRow=[pickerTypesCarburants selectedRowInComponent:0];
NSString *item=[typesCarburantsArray objectAtIndex:selectedRow];
typeCarburantTextField.text=[NSString stringWithFormat:#"%#",item];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 480);
pickerViewTypesCarburants.transform=transform;
[UIView commitAnimations];
}
-(IBAction)pickerTypeCarburantsShow{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 240);
pickerViewTypesCarburants.transform=transform;
[self.view addSubview:pickerViewTypesCarburants];
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
First of all, you probably shouldn't be trying to make a UIPickerView appear when you tap on a UITextField as it isn't standard behaviour (especially if you're suppressing the keyboard). It sounds like you need a standard UIButton that when pressed presents the UIPickerView as this would make more sense.
Regardless, if you aren't seeing Datasource and Delegate outlets in IB, try applying them manually in your code.
pickerTypesCarburants.delegate = self;
pickerTypesCarburants.dataSource = self;
To answer your second question, the keyboard will only hide when you press the return key with the textFieldShouldReturn method you have implemented. The UITextField will also have to have its delegate set (I'm assuming you're doing this in IB as not listed in your .m file). To make the keyboard hide as soon as you press the UITextField, you would to alter your pickerTypeCarburantsShow method:
-(IBAction)pickerTypeCarburantsShow{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 240);
pickerViewTypesCarburants.transform=transform;
[self.view addSubview:pickerViewTypesCarburants];
[UIView commitAnimations];
[typeCarburantTextField resignFirstResponder];
}
This will make sure that the keyboard is hidden immediately (rather than on pressing the return button). Again, I'd question why you would want to make a UIPickerView appear when tapping a UITextField as it probably goes against the Human Interface Guidelines.

Resources