I've been following this and a bunch of other tutorials/blogs,etc trying to get my view to scroll when the user hits "return" and ends on a UITextField that is being covered by the keyboard but nothing is actually working.
I'm wondering what must I be doing or missing that is causing this?
Basically:
the user wants to do something in the app: add a credit card
show a UIView that contains all the CC fields
1/2 of the UITextFields are covered by the keyboard
scroll the view when the user gets there.
Nothing is happening. The code for detecting the 'covered by keyboard' case is being hit and executed but: [self.scrollView setContentOffset:scrollPoint animated:YES] has no effect at all.
Thoughts?
Reveal screenshot:
code: same as the tutorial in the link. Nothing new there.
Did you check the contentSize property?
To make a scroll view scrollable, the content must be larger than the display area(usually the bounds of the scrollview).
You can achieve this by explicitly setting the contentSize property which is CGSizeZero by default
In addition, setting the contentInset property to adjust the size of display area(in this case you can set bottom value equals keyboard height) when the keyboard is popped up
Try this,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y-keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
and
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y+keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
What I do to avoid the textField being covered by the keyboard is to animate the textfield to the top of the screen when the user touches it to start entering something with the keyboard and animate it back when done. It is much more elegant in my opinion. Maybe that could be an alternative solution for you... I do it for the different screensizes with different numbers... you might need to adjust it of course...
So listen to the textField delegate and in
- (void)textFieldDidBeginEditing:(UITextField *)textField {
you do
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 568){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 667){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -238; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 736){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -251; [self.view setFrame:frame]; [UIView commitAnimations];});
}
and in
- (void)textFieldDidEndEditing:(UITextField *)textField {
you just animate back
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = 0; [self.view setFrame:frame]; [UIView commitAnimations];});
Related
I am trying to use this code
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%f",self.view.frame.origin.y);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25f];
CGRect frame = self.view.frame;
frame.origin.y =frame.origin.y -204;
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25f];
CGRect frame = self.view.frame;
frame.origin.y = frame.origin.y + 204;
[self.view setFrame:frame];
[UIView commitAnimations];
}
To move view up when keyboard is present but this results like this:
becomes this:
Can't figure out why and if it has to do something with the navbar or constraints.
Why not to use this beautiful library named IQKeyboardManager. Just add it through pods/manually and let it do your work
Kind of what Alexey Sobolevsky said did the trick. Basically i needed to put them in a scrollview and add a constraint from the scroll view to the object beneath, then put all the views containing textfields inside that scrollview instead. This made everything work correctly.
First add the scroll view.
Then write the following code.
//When return key press then bring the view back
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
self.scrlView.contentOffset = CGPointMake(0, 0);
return YES;
}
//When the text field begin editing then moves the view up
-(void)textFieldDidBeginEditing:(UITextField *)textField{
self.scrlView.contentOffset = CGPointMake(0, 30);
}
//When ext field end editing then bring the view back
-(void)textFieldDidEndEditing:(UITextField *)textField{
self.scrlView.contentOffset = CGPointMake(0, 0);
}
Update, SOLVED
Sorry for my english, but i will do my best.
I have a problem with a button flip animation.
I want the UIButtons to fill the UIView. But how do i do that?
Here is my problem.
I have two UIButtons in a UIView.
When i press the first button it will flip correctly but when i press the second button that it have flipped to the UIView is still the same size but the UIButton image is changing size to the original size of the image and it also move the image to the upper left corner off the view controller. The same thing will repeat every time it flip.
Here is the flip animation code.
- (IBAction) buttonPressedFlip {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
if (flipState == 0) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainer cache:YES];
[self.btn1 removeFromSuperview];
[buttonContainer addSubview:btn2];
flipState = 1;
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:buttonContainer cache:YES];
[self.btn2 removeFromSuperview];
[buttonContainer addSubview:btn1];
flipState = 0;
}
[UIView commitAnimations];
}
Have a nice day and thanks for any help!
I solved the problem like this
- (IBAction) buttonPressedFlip {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
if (flipState == 0) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:buttonContainer cache:YES];
[self.buttonContainer bringSubviewToFront:btn2];
flipState = 1;
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainer cache:YES];
[self.buttonContainer bringSubviewToFront:btn1];
flipState = 0;
}
[UIView commitAnimations];
}
I solved the problem
See my update code.
im trying to do an animation on UIPicker where when my app runs its hidden and when the button is pressed it will appear sliding from the buttom and when i tap the button again it will slide away. well, my code does that but only just once.
it does:
first click -> show the picker, isPickerHidden = NO
second click -> hides picker, isPickerHidden = YES
third click -> does not do anything, but it returns isPickerHidden = NO
where in viewDidLoad declaration are;
and same as the fourth click it doesnt do anything but return the correct BOOL value.
isPickerHidden = YES;
[self.picker setHidden:isPickerHidden];
if(isPickerHidden == NO){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGRect frame = self.picker.frame;
[self.picker setFrame:CGRectOffset(frame, self.picker.frame.origin.x, self.picker.frame.origin.y)];
[UIView commitAnimations];
isPickerHidden = YES;
NSLog(#"hidden yes");
}else if(isPickerHidden == YES) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, -200);
self.picker.transform = transfrom;
[self.picker setHidden:NO];
[UIView commitAnimations];
NSLog(#"hidden no ");
isPickerHidden = NO;
}
i would like to know on whats the problem i am facing, and what am i missing. thanx
add some logging for the frame and picker to see what is happening...
NSLog(#"picker: %.0f, %.0f, %.0f, %.0f", self.picker.frame.origin.x, self.picker.frame.origin.y, self.picker.frame.size.width, self.picker.frame.size.height);
thanx NS bro for the reply but i did a different approach now instead of offsetting the points from the original one i used CGRectMake, i did this
if(isPickerHidden == NO) {
CGRect frame = self.picker.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.6];
[self.picker setFrame:CGRectMake(frame.origin.x, frame.origin.y + 216, frame.size.width, frame.size.height)];
[UIView commitAnimations];
isPickerHidden = YES;
} else if (isPickerHidden == YES) {
CGRect frame = self.picker.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.6];
[self.picker setFrame:CGRectMake(frame.origin.x, frame.origin.y - 216, frame.size.width, frame.size.height)];
[UIView commitAnimations];
isPickerHidden = NO;
}
both the sliding down and sliding up are doing well animated in the same manner. but now the problem is i wanted to make the UIPicker disappear on app load so in the viewDidLoad i did try to use the [self.picker setHidden:YES]; but the frames i made using CGRect dont appear when i press the button and i also tried alpha still nop.
i did also put in the body of the if-else statement the setHidden to yes and to know with respect to their values.
would like to know what im missing now.
I have developed a font selection that utilizes UIPickerView and UIToolbar, which are both added on touching a UIButton.
But they just kind of pop in which looks sloppy.
is there a way to animate them?
here is the method that adds them (it's called on clicking the button) :
-(void)showPicker
{
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
}
You can animate them in a couple of ways, but probably the easiest is to just fade them in.
Fade In
[someView setAlpha:0]
[self.view addSubview:someView];
[UIView beginAnimations:#"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:1];
[UIView commitAnimations];
Fade Out
float fadeDuration = 0.5;
[UIView beginAnimations:#"FadeOut" context:nil];
[UIView setAnimationDuration:fadeDuration ];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:0];
[UIView setAnimationDidStopSelector:#selector(removeView)];
[UIView commitAnimations];
-(void) removeView {
[someView removeFromSuperview];
}
Something as simple as that.
Depends on what type of animation you want to use. For example, this would look like dissolve.
Anyways look into UIView animateWithDuration...
pickerTB.alpha = _fontPicker.alpha = 0;
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
[UIView animateWithDuration:1 animations:^{_fontPicker.alpha = 1; pickerTB.alpha = 1}];
You could use transforms or change frame origins to make the views fly in from positions outside the screen too. In that case, inside the animations block, specify the new on screen position. You can do this in two ways, change the frame with an updated origin or apply a CGAffineTransformMakeTranslation(dx,dy)
You need to add the subview with a frame value that is where you want the animation to begin from (off-screen?) then change the frame value inside your animation block. The frame will change over the duration, causing the appearance of movement. The view must already be a subview - I don't believe adding a subview is something you can animate, it makes no sense.
-(void) showPicker{
[self.view addSubview: _fontPicker];
_fontPicker.frame = CGRectMake(0, 0, 120, 40);// somewhere offscreen, in the direction you want it to appear from
[UIView animateWithDuration:10.0
animations:^{
geopointView.frame = // its final location
}];
}
Initially set your picker view frame and tool bar frame like this one..
-(void)viewDidLoad
{
[super viewDidLoad];
pickerTB.frame = CGRectMake(0,568,320,44);
fontPicker.frame = CGRectMake(0, 568, 320, 216);
}
-(void)showPicker
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.30];
if(isiPhone5)
{
pickerTB.frame = CGRectMake(0,288,320,44);
fontPicker.frame = CGRectMake(0, 332, 320, 216);
}else{
pickerTB.frame = CGRectMake(0,200,320,44);
fontPicker.frame = CGRectMake(0,244, 320, 216);
}
[UIView commitAnimations];
}
In my iPhone app, I have two views on a main view controller , one is normal view 'a' with some buttons and other is scrollView 'b'. When I tap on button in view 'a', I want to show scroll view with animations like the scroll view has to come up from the back of view 'a', but it comes up from the front of view 'a'.
I used the following code for animating the scroll view.
CGRect Frame = scrollView.frame;
if(Frame.origin.y == 420){
Frame.origin.y = 298;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
}];
}else{
Frame.origin.y = 420;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
}];
How to implement the animation to scroll view to show it from top of view 'a'.
Try to move "b" to the back first:
[self.view sendSubviewToBack:scrollview_b];
I found solution for my problem.
Solution :
CGRect Frame = scrollView.frame;
if(Frame.origin.y == 420){
Frame.origin.y = 298;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
[self.view insertSubview:scrollView belowSubview:view_a];
}];
}else{
Frame.origin.y = 420;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
[self.view insertSubview:scrollView belowSubview:view_a];
}];
}