How to stop reloadData from moving a table view - ios

I have a table view that I animate to a new position when pressing a button. It covers a small portion of the screen at the bottom and then pressing the button calls a method that makes it cover about half the screen. This way the user can choose a file from the table view.
However, it seems that when I call reloadData, the table view moves back to its original position, and I would like to stop that from happening so that the user doesn't need to press the button every time (such as with deleting a file, renaming a file, or adding a new file to the table view).
Any ideas how I could accomplish this?
I've included code. The file IBAction is what happens when the button is pressed.
-(IBAction)file:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
if (self.tableView.frame.origin.y == 457) {
[self tableAppear];
}
else {
[self tableDisappear];
}
}
-(void)tableDisappear {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
if ([[UIScreen mainScreen] bounds].size.height == 568) { [self.tableView setCenter:CGPointMake(160, 545)];
}
else { [self.tableView setCenter:CGPointMake(160, 545)];
}
[UIView commitAnimations];
}
-(void)tableAppear {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
if ([[UIScreen mainScreen] bounds].size.height == 568) { [self.tableView setCenter:CGPointMake(160, 390)];
}
else { [self.tableView setCenter:CGPointMake(160, 390)];
}
[UIView commitAnimations];
}

This is most likely a consequence of auto layout (when the screen needs to be redrawn, all views will revert to the positions determined by their constraints rather then any frames that you set). You can turn off auto layout to see if this is the problem. If you want to use auto layout, then you need to do any moving or resizing of view using constraints rather than setting views.

Related

IOS stop UIbutton to take its own place after animation completed

I have set the animation like seen in below image. In which UIbutton move from left to right and then top to bottom. Animation work correctly but after completion of animation UIButton comes to its original place before the segue perform. so, it's not look good. I want to set that after the completion of animation UIButton can't come to it's own place before segue .
Here is my try with Image.
//Move button Left to Right
- (IBAction)btnEasy:(id)sender {
Easy=YES;
NSLog(#"your x is: %f ", self.btnEasy.frame.origin.x);
NSLog(#"your y is: %f ", self.btnEasy.frame.origin.y);
x1=self.btnEasy.frame.origin.x;
y1=self.btnEasy.frame.origin.y;
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView animateWithDuration:1.150 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.btnEasy.frame = CGRectMake(screenRect.size.width/1.80, self.btnEasy.frame.origin.y, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height);
[self performSelector:#selector(btneasyanimation) withObject:self afterDelay:1.160 ];}
completion:^(BOOL finished) {
}];
//Move Button top to Bottom
if (Easy==YES) {
if (isiPad2 || isiPadAir || isiPadRatina) {
//[self.view layoutIfNeeded];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1.0];
[_btnEasy setFrame:CGRectMake(self.view.frame.origin.x+290, self.view.frame.origin.y+900, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height)];
[UIView commitAnimations];
}
else if (isiPhone4s) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1.0];
[_btnEasy setFrame:CGRectMake(self.view.frame.origin.x+92, self.view.frame.origin.y+428, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height)];
[UIView commitAnimations];
}
}
[self performSelector:#selector(segueeMethod) withObject:self afterDelay:1.160 ];
Image :-
If you are not placing directly the button given it the frame, thus using autolayout (autoresize will end with the same effect). You need to explicitly use autolayout, retain a reference to your constraints and update them (you can search here how to do that) and then set the UIView animation block [button layoutIfNeeded]
You can see a detailed answer about it in this answer
There is an easy and quick way that will work with your current implementation. Provided you know exactly where you want the view to be once the animation is done, you can do the following:
in UIView animateWithDuration: ... assign the final transform (position & orientation) of the view in the completion block, i.e.:
completion:^(BOOL finished) {
// Assign views transform and appearance here, for when the animation completes
}
Hope that helps.

scrollView.setContentOffset does not scroll the view, at all

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];});

UIView Will Not Resize

My storyboard elements are subviews of containerView and containerView is a subview of the main view. I am trying to resize the height of my container view when an ad is available to show but I cannot get that to work. I am able to offset the view up, but I am not able to resize it. I have seen quite a bit a posts about this, but all suggestions I've read basically say to confirm Autolayout is not checked. I have confirmed that Autolayout is not checked in each element of my storyboard but still am not having success with this. The ad banner (placed just off screen at [0, 480] pops up nicely from the bottom just like I want it to, but it covers up my storyboard elements which is just plain UNACCEPTABLE. I will not stand for this! I need some help guys and gals…Please see code below:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
_containerView.frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
Try it like this:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
CGRect frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
[_containerView setFrame:frame];
CGRect frame2 = banner.frame;
frame2.origin.x = 0;
frame2.origin.y = -50;
[banner setFrame:frame2];
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
You have just changed the frame of your containerView. This does not in any way change the frames of the subviews of containerView. You either have to make containerView a scrollview or change the frames of every component.

IOS: View Translation Animation. Proper way.

I'm implementing my own Navigation Controller and I'm faking the animation of adding the new view as in UINavigationContoller.
The following code works great, however if I move -addSubview:, before UIView animation call, it will animate in ios7, but not in ios6, the question is why? (I mean, why does it would make a difference, as animation will be scheduled and executed asynch, right? Or I might suppose It will somehow affect the starting state of animation?)
- (void)didAddViewControllerInNavigationStack:(NSArray*)navigationStack
{
if (self.activeViewController) {
[self.activeViewController viewWillDisappear:YES];
[self.activeViewController viewDidDisappear:YES];
}
self.activeViewController = navigationStack.firstObject;
if (!self.activeViewController) return;
CGRect windowRect = [[UIScreen mainScreen] bounds];
windowRect.origin.x = windowRect.size.width;
self.activeViewController.view.frame = windowRect;
[self.activeViewController viewWillAppear:YES];
[UIView transitionWithView:self.view
duration:0.32
options:UIViewAnimationOptionCurveEaseOut
animations:^ { self.activeViewController.view.frame = [[UIScreen mainScreen] bounds]; }
completion:^(BOOL isDone){[self.activeViewController viewDidAppear:YES];}];
[self.view addSubview:self.activeViewController.view];
[UIView commitAnimations];
}
From the documentation of transitionWithView:duration:options:animations:completion:, it is said you should add the subview inside the animation block:
The block you specify in the animations parameter contains whatever state changes you want to make. You can use this block to add, remove, show, or hide subviews of the specified view.
Also, you are calling commitAnimations without having called beginAnimations:context:. You are mixing things here. Block-based animation API does not need the begin-end API.
Your code should be:
[UIView transitionWithView:self.view
duration:0.32
options:UIViewAnimationOptionCurveEaseOut
animations:^ { [self.view addSubview:self.activeViewController.view]; self.activeViewController.view.frame = [[UIScreen mainScreen] bounds]; }
completion:^(BOOL isDone){[self.activeViewController viewDidAppear:YES];}];

UIView disappears after orientation change

I have the current problem:
From an NSObject i'm creating / loading and animating a view:
[UIView beginAnimations:#"slidein" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[content.view setAlpha:1.0];
for (UIView *view in content.view.subviews) {
[view setAlpha:1];
}
[content.view setFrame:CGRectMake(0, 0, self.contentEnclosure.size.width, self.contentEnclosure.size.height)];
[UIView commitAnimations];
My problem is that, when orientation changes, my animated view disappears. If i make my view appear again, the orientation settings (frame size) are correct, so i'm guessing, that the resizing mask is applied correctly, i just don't understand what needs to be done, for my view not to disappear.
Use NSLog and check the new self.view.frame.origin.x and y respectively after the orientation.
Then set the frame according to the requirement.
Also, check for resizing mask. Try using UIViewAutoresizingFlexibleRightMargin/LeftMargin to suit your requirements.

Resources