View Controller Animation in Xcode - objective-c

I need to animate a view when user click on the button. i am using the following code for animating
App Delegate
ViewController2 *vc2_controller = [[ViewController2 alloc]init];
UINavigationController *baseViewController = [[UINavigationController alloc]initWithRootViewController:vc2_controller];
[self.window addSubview:baseViewController.view];
self.window.rootViewController = vc2_controller;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
self.viewController = [[vc1controller alloc]initWithNibName:nil bundle:nil];
//ViewController1.m -- This is Working
[self.navigationController pushSlideViewController:self.navigationController];
I created function definition inside the category
//Category
- (void)pushSlideViewController:(UIViewController *)viewController
{
// NSLog(#"In Navigation Controller");
//[UIView transitionWithView:viewController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
CGRect screensize = [[UIScreen mainScreen]bounds];
[UIView animateWithDuration:0.5 animations:^{
NSLog(#"In View ANimatin");
CGRect current_rect = viewController.view.frame;
viewController.view.frame = current_rect;
current_rect.origin.x = screensize.size.width-50;
viewController.view.frame = current_rect;
}];
}
Now i am calling ViewController1.m from ViewController2.m -- Animation is Not Working
ViewController1 *view_control = [[ViewController1 alloc]init];
[self.navigationController popSlideViewController:view_control];
//Category
-(void)popSlideViewController:(UIViewController *)viewController
{
CGRect screensize = [[UIScreen mainScreen]bounds];
//NSLog(#"In Pop");
[UIView animateWithDuration:0.5 animations:^{
//View is not coming to original position.
CGRect current_rect = viewController.view.frame;
NSLog(#"In View ANimatin %f",current_rect.origin.x);
viewController.view.frame = current_rect;
current_rect.origin.x = 0.0;
viewController.view.frame = current_rect;
}];
}
Note: i am not using ios5 :-)
Thanks,

You're loading VC2 from VC1, which, as you say, works fine. Then you're trying to load VC1 from VC2, which is where the problem lies - VC1 is still loaded, behind VC2, so you just need to dismiss VC2 in a similar way to how you brought it up in the first place, just animate it back off the screen, remove it from the view, then release it (unless you released it when you showed it). Alternatively you could use the built-in methods,
presentViewController:animated:completion:
and
dismissViewControllerAnimated:completion:
which have their own animation.

Related

UIPopoverPresentationController can not be dismissed on iPhone

I'm implementing a CABTMIDICentralViewController (Apple's pre-fab BTLE MIDI configuration panel). The code below is Apple's sample code - unmodified.
It works perfectly on iPad, but on iPhone/iPod it results in an uncloseable fullscreen view. The code clearly creates a Done button, but it isn't shown on the devices.
The common answer is "you need a UINavigationController", but there is one being made in this code. So I'm not sure what else is missing?
- (void)doneAction:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)configureCentral:(id)sender
{
CABTMIDICentralViewController *viewController [CABTMIDICentralViewController new];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
// this will present a view controller as a popover in iPad and modal VC on iPhone
viewController.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doneAction:)];
navController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popC = navController.popoverPresentationController;
popC.permittedArrowDirections = UIPopoverArrowDirectionAny;
popC.sourceRect = [sender frame];
UIButton *button = (UIButton *)sender;
popC.sourceView = button.superview;
[self presentViewController:navController animated:YES completion:nil];
}
You will have to implement the UIPopoverPresentationControllerDelegate to view popovers in iPhones. By default it will be presented in the style of an already presented view controller.
Add this piece of code to present the controller as popover
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(nonnull UITraitCollection *)traitCollection {
return UIModalPresentationNone;
}

Objective C Navigation Bar does not adjust when StatusBar changes

We have a problem where the user is using our app on the iPhone and receives a call while on a view that has been presented by the rootviewcontroller and is covering the navigation controller and it's nav bar. The status bar for the call shows and pushes the current view down but when that view is removed from the superview, the status bar covers half of the navigation bar.
We have tried using a UIApplicationWillChangeStatusBarFrameNotification in the app delegate and then adjusting the size of the navigation controller and the navigation bar to no effect. We have also tried resetting the frame of the navigation controller and nav bar in the viewWillAppear function of the page that presents the view.
Can anyone tell us where we are going wrong?
In App Delegate we tried this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupAppStyles];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor ugBlue];
UINavigationController *navController = [[UINavigationController alloc] init];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
[self setUpNotifications];
return YES; }
-(void) setUpNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(statusFrameChanged:)
name:UIApplicationWillChangeStatusBarFrameNotification
object:nil];}
- (void)statusFrameChanged:(NSNotification*)note {
CGRect statusBarFrame = [note.userInfo[UIApplicationStatusBarFrameUserInfoKey] CGRectValue];
self.statusHeight = statusBarFrame.size.height;
UIScreen *screen = [UIScreen mainScreen];
CGRect viewRect = screen.bounds;
viewRect.size.height -= self.statusHeight;
viewRect.origin.y += self.statusHeight;
[[UINavigationBar appearance] setFrame:viewRect];
NSLog(#"The status frame has changed");
//[self.navController.view setFrame:viewRect];
self.navController.view.frame.size.height);}
We have also tried something similar in the views viewWillAppear functions also with no positive results. Any thoughts?
We were able to solve this problem. In the viewDidAppear of the view that called presentViewController we put
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGRect frame = CGRectMake(0.0f, statusSize, screenRect.size.width, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
When the status bar is shown

UISplitviewController(window.rootViewController) viewcontrollers not deallocated on iOS8 after exchanging it with UINavigationController

So I have this problem.
I have a simple test project in which i have an UINavigationController as my window.rootViewController.
In the navigationBar I have a button which executes following code:
MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil] autorelease];
UINavigationController *masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
DetailViewController *detailViewController = [[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
masterViewController.detailViewController = detailViewController;
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = #[masterNavigationController, detailNavigationController];
[UIView transitionWithView:self.window
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
self.window.rootViewController = self.splitViewController;
[UIView setAnimationsEnabled:oldState];
}
completion:nil];
Pressing this button will as the code shows, simply replace the existing rootViewController with an UISplitViewController.
After pressing the button everything is fine. The UISplitViewController is my rootViewController.
After pressing a specific Button in the UISpliViewController following code will be executed:
[UIView transitionWithView:self.window
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
self.window.rootViewController = self.navCTL;
[UIView setAnimationsEnabled:oldState];
}
completion:^(BOOL finished) {
self.splitViewController = nil;
}];
This code simply does the reverse of which was done with the above button.
When pressed the rootViewController will be replaced.
Now here's the awkward thing.
On iOS7 and less the viewControllers of the UISplitViewController will be deallocated, after pressing the second button.
That's how i want it and how i would expect it.
On iOS8 it's different. After pressing the second button they won't be deallocated, until i once again press the first button and the rootViewController will be a UISplitViewController (a new one) again.

transitionFromView:toView:duration:options:completion: is not animating the transition

I have a view controller that displays the views of 2 sub view controllers in a given area of its view. The 2 sub view controllers are FlopVC and FipVC.
I want to animate the transition from one sub view to the other. The code I'm using is:
-(IBAction)flip:(id)sender{
UIViewController *newVC = nil;
if (self.isFlip) {
newVC = [[FlopVC alloc] initWithNibName:nil bundle:nil];
}else{
newVC = [[FipVC alloc] initWithNibName:nil bundle:nil];
}
newVC.view.frame = CGRectMake(120, 20, 240, 260);
[self.view addSubview:newVC.view];
[UIView transitionFromView:self.currentVC.view
toView:newVC.view
duration:0.9
options:UIViewAnimationTransitionFlipFromLeft
completion:^(BOOL finished) {
self.currentVC = newVC;
self.isFlip = ! self.isFlip;
}];
}
The sub views are swapped, but without any animation. What am I doing wrong?
PS the full project is here.
UIViewAnimationTransitionFlipFromLeft != UIViewAnimationOptionTransitionFlipFromLeft
if you are using the new iOS5 view container paradigm you need to do something along the lines of the following:
-(IBAction)flip:(id)sender{
UIViewController *newVC = nil;
if (self.isFlip) {
newVC = [[FlopVC alloc] initWithNibName:nil bundle:nil];
}else{
newVC = [[FipVC alloc] initWithNibName:nil bundle:nil];
}
newVC.view.frame = CGRectMake(120, 20, 240, 260);
// required for the new viewController container
self.currentVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
[self transitionFromViewController:self.currentVC
toViewViewController:newVC.view
duration:0.9
options: UIViewAnimationOptionTransitionFlipFromLeft
animations:nil
completion:^(BOOL finished) {
// required for the new viewController container
[self.currentVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
self.currentVC=newVC;
}];
}
reference the section Implementing a Container View Controller and the 2011 WWDC videos on UIViewController containers for more info.
Here is working code that (by sheer coincidence) does exactly what you're describing. My two child vc's are stored in self->swappers. The integer cur keeps track of which one is currently showing. The spot in my interface where the subview is to go is marked by a view outlet, panel.
UIViewController* fromvc = [self->swappers objectAtIndex:cur];
cur = (cur == 0) ? 1 : 0;
UIViewController* tovc = [self->swappers objectAtIndex:cur];
tovc.view.frame = self.panel.bounds;
// must have both as children before we can transition between them
[self addChildViewController:tovc]; // "will" called for us
// note: when we call remove, we must call "will" (with nil) beforehand
[fromvc willMoveToParentViewController:nil];
[self transitionFromViewController:fromvc
toViewController:tovc
duration:0.4
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:nil
completion:^(BOOL done){
// note: when we call add, we must call "did" afterwards
[tovc didMoveToParentViewController:self];
[fromvc removeFromParentViewController]; // "did" called for us
}];

Adding a TabBarController as the Subview of a View

I am loading a splash screen when my app starts. Then I want to load a TabBarController and it's ViewControllers. However, my TabBarController window does not scale to the screen size.
Probably 3/4 of the TabBar at the bottom is getting cut off and There is a slim aprox 20 pixel gap at the top of the screen below the status bar. How do I resize the TabBarController properly?
Here is the code in my SplashViewController loading the splash view, and the TabBarController:
-(void)loadView{
// Init the view
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView *view = [[UIView alloc] initWithFrame:appFrame];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = view;
[view release];
splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Splash.png"]];
splashImageView.frame = CGRectMake(0,0,320,458);
[self.view addSubview:splashImageView];
viewController = [[FlashCardViewController alloc] initWithNibName:#"FlashCardViewController" bundle:[NSBundle mainBundle]];
//viewController.view.bounds = [[UIScreen mainScreen]bounds];
viewController.title = #"Quiz";
viewController.tabBarItem.image = [UIImage imageNamed:#"puzzle.png"];
UIViewController *viewController2 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewController2.title = #"Nada";
viewController2.tabBarItem.image = [UIImage imageNamed:#"magnifying-glass.png"];
//viewController.view.alpha = 0.0;
//[self.view addSubview:viewController.view];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController, viewController2, nil];
[viewController2 release];
tabBarController.view.alpha = 0.0;
//tabBarController.tabBarItem.image = [UIImage imageNamed:#"State_California.png"];
//tabBarController.tabBarItem.title = #"State_California.png";
tabBarController.view.bounds = [[self view] bounds];
//tabBarController.view.frame = [[UIScreen mainScreen] applicationFrame];
[self.view addSubview:tabBarController.view];
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(fadeScreen) userInfo:nil repeats:NO];
}
-(void) fadeScreen
{
[UIView beginAnimations:nil context:nil]; // begin animation block
[UIView setAnimationDuration:0.75]; // sets animation duration
[UIView setAnimationDelegate:self]; // sets the delegate for this block
[UIView setAnimationDidStopSelector:#selector(finishedFading)]; // Calls finishFading
self.view.alpha = 0.0; // // Fades the alpha to 0 over animation
[UIView commitAnimations]; // Commits this block, done
}
-(void) finishedFading
{
[UIView beginAnimations:nil context:nil]; // Begin animation block
[UIView setAnimationDuration:0.75]; // set duration
self.view.alpha = 1.0; // fades the view to 1.0 alpha over .75 seconds
//viewController.view.alpha = 1.0;
tabBarController.view.alpha = 1.0;
[UIView commitAnimations];
[splashImageView removeFromSuperview];
}
I've just completed pretty much the same and ran into the same problems but eventually I got it working.
Create a View Controller class in Xcode called Test1ViewController and add the following:
#interface Test1ViewController : UIViewController {
IBOutlet UITabBarController *tbc;
}
#property (nonatomic,retain) IBOutlet UITabBarController *tbc;
#end
Create a View XIB called Test1View
Add a TabBarViewController to the XIB
Set the File's Owner in the XIB to be the Test1ViewController.
Connect the tbc IBOutlet in the File's Owner to the Tab Bar Controller in the XIB.
Connect the view IBOutlet in the File's Owner to the View in the XIB.
In your SplashViewController.h add the property
Test1ViewController *tabBarViewController;
Synthesize the tabBarViewController in your SplashViewController.m.
Replace your TabBarController creation code in your loadView method in SplashViewController with the following:
tabBarViewController = [[Test1ViewController alloc] initWithNibName:
#"Test1View" bundle:[NSBundle mainBundle]];
tabBarViewController.view.alpha = 0.0;
[self.view addSubview:[tabBarViewController view]];
Here's the bit that was missing for me. In Test1ViewController.m, you need to add the following line to the viewDidLoad method:
self.view = tbc.view;
Finally, I also had to change the finishedFading method in SplashViewController.m to set the alpha to 1.0 on the tabBarViewController view.
-(void) finishedFading
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
self.view.alpha = 1.0;
tabBarViewController.view.alpha = 1.0;
[UIView commitAnimations];
[splashImageView removeFromSuperview];
}
I hope this helps.
I finally found someting that works. Instead of:
tabBarController.view.frame = [[UIScreen mainScreen] applicationFrame];
or
tabBarController.view.bounds = [[self view] bounds];
Because I couldn't find and automatic or named settings for this size, I had to create my own rectangle that is the size of the screen minus the statusBar.
tabBarController.view.frame = CGRectMake(0,0,320,460);