UINavigationController — left and right flip animation between pushes and pops when presented via presentViewController - objective-c

Supposed you've got:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view.backgroundColor = [UIColor whiteColor];
[self.window setRootViewController:rootViewController];
[self.window makeKeyAndVisible];
UINavigationController *modal = [[UINavigationController alloc] initWithRootViewController:[[PTFrontViewController alloc] init]];
modal.modalPresentationStyle = UIModalPresentationFormSheet;
[rootViewController presentViewController:modal animated:YES completion:NULL];
return YES;
}
whereas PTFrontViewController and PTBackViewController view controllers have nothing interesting for sake of this example.
How could you push an instance of PTBackViewController from PTFrontViewController animating as in UIViewAnimationTransitionFlipFromLeft or UIViewAnimationTransitionFlipFromRight?
I am already well aware of these three things:
this is not exactly how you should make use of presentViewController
there is a good reason for UINavigationController's default animation
there are several answers how to "customize" UINavigationController's default animation while pushing and poping, but if you try the code for your self you will notice that when a view controller is presented via presentViewController there are drop shadows and background views that won't get animated correctly
So please answer taking these things in mind. Thank you.

First - forget UINavigationController. If you don't need the default animation, just put a UINavigationBar into your controllers. It will get a little easier.
Second - this is a difficult problem, you can't create such an animation only within the modal controller because the background wouldn't be repainted.
Sincerely, the easist solution I see is too forget the modal controller and just add the view controller as a child of your root controller. Then you can control all the animations but you have to write everything by yourself (including the background fading).

Related

UIViewController loading a UISplitViewController

I have a UINavigationController as my rootViewController, which contains a UIViewController (which I will call projects for the sake of this discussion). In Projects, I have a button, which when clicked, I want to load a UISplitViewController - preferably sliding up from the bottom, although this is just a nice-to-have feature.
In the UISplitViewController, I have a "Close" button which I want to remove the UISplitViewController re-showing Projects.
From what I have read, UISPlitViewControllers must be the rootViewControllers. With that in mind, my code so far is as follows.
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
...
// LOAD THE PROJECTS PANEL ROOT VIEW CONTROLLER INTO THE WINDOW
ProjectsListViewController *projects = [[ProjectsListViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:projects];
[window setRootViewController:navigationController];
[window makeKeyAndVisible];
return YES;
}
ProjectsViewController
-(IBAction)loadDetails
{
ProjectNavigationController *projectNavPanel = [[ProjectNavigationController alloc] init];
ProjectDetailController *projectDetailPanel = [[ProjectDetailController alloc] init];
ProjectSplitViewController *splitRootController = [[ProjectSplitViewController alloc] init];
[splitRootController setViewControllers:[NSArray arrayWithObjects:projectNavPanel, projectDetailPanel, nil]];
[[self view] removeFromSuperview];
[[appDelegate window] setRootViewController:splitRootController];
}
UISplitViewController Naviagaion
- (void)loadProjects
{
// LOAD THE PROJECTS LIST BACK INTO VIEW
ProjectsListViewController *projectsList = [[ProjectsListViewController alloc] init];
[[[self parentViewController] view] removeFromSuperview];
[[appDelegate window] setRootViewController:projectsList];
}
Now, I know this is wrong, and unsurprisingly it is having adverse effects on other methods. In fact, as I type this, I noticed that the Projects page is being loaded in a navigationController on launch, but placed directly on the window when the splitViewController is closed. Can anyone help me by explaining the correct method of achieving this?
Thanks
Apple say that a UISplitViewController must be the topmost view controller in your app, and that it must be there for the entire lifetime of your app. As you've noticed, if you ignore this, everything can break.
There are some alternatives out there that don't break this way, e.g. MGSplitViewController. Google around. If you have the time, you could even cook your own implementation of a split view controller and be in complete control.
If you really want to use Apple's UIsplitViewController in "crazy" ways, then you can install it as root VC (as Apple demand), and have it at the root all the time, but then show other UIs modally over the top of it. Then hide the modal UI to make the split view controller appear. This is nasty and hacky though.
A while back I asked a related question that may be of interest:
Best way to switch between UISplitViewController and other view controllers?

Tab Bar disappearing when switching views

I was hoping that you could help me with a small issue.
I have a program that has a tab bar controller with 5 tab bar items. On the main page this tab bar appears and I can switch between the 5 tab bar items with no issues.
On some views I have a button which loads a xib file, some of which are xibs that are selectable from the tab bar. However, whenever I load a view using a button I lose the tab bar - and this is my issue.
My current way of loading between views is as follows:
- (IBAction)newGamePressed
{
NewGameIntro *screen = [[NewGameIntro alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
}
This is probably quite an obvious solution but I am pretty new to Objective-C and searching the forum I couldn't find anything particularly obvious.
Any help that anyone can give would be much appreciated.
Thanks in advance!
PS The way I loaded the TabBarController was as follows (if this helps) in the delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[NSBundle mainBundle] loadNibNamed:#"TabBarController" owner:self options:nil];
[self.window addSubview:rootController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
You are presenting a modal view which will cover the entire window as it is designed to. If your goal is to simply push a new controller onto the stack you will need your tab's root views to be a UINavigationController upon which you will simply push another view using
[self.navigationController pushViewController:...]

Why is the view from my XIB file not being loaded by the UIViewController?

In my app delegate, I have:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[CGMContainerViewController alloc] init];
self.window.rootViewController = self.viewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
I have an unmodified CGMContainerViewController.h/m and a modified CGMContainerViewController.xib (added a button). The view is linked to the File Owner.
Why is the view from the XIB not showing?
EDIT: Is it because I'm trying to load a view with a status bar into the window?
From the looks of your code, you aren't loading any XIB files. You need to call initWithNibName:
self.viewController = [[CGMContainerViewController alloc] initWithNibName:
#"CGMContainerViewController" bundle: nil];
Make sure that your File's Owner in the XIB is set to CGMContainerViewController
Okay. I think it was because for a UINavigationController, you have to set the rootViewController and all the layers. I just changed it to a UIViewController because I didn't really need the navigation, just the navigation bar.
The problem which I faced is .If you name your viewController with special character with "-" and some other things your xib will not get loaded
example--> (wrong naming)
view-controller1 (If naming is like this then the xib wont get loaded)
example--->(correct naming)
viewController1 (xib will get loaded)

What is the difference between the two statements below?

self.window.rootViewController = self.tabBarController;
[self.window addSubview:self.tabBarController.view];
They are used in the context below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
// self.window.rootViewController = self.tabBarController;
[self.window addSubview:self.tabBarController.view];
IntroViewController *introViewController = [[IntroViewController alloc] initWithNibName:#"IntroViewController" bundle:nil];
//Lets place introViewController in navController
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:introViewController];
//Now lets display it
[self.tabBarController presentModalViewController:navController animated:YES];
[navController release];
[introViewController release];
[self.window makeKeyAndVisible];
return YES;
}
From iOS Reference:
rootViewController
The root view controller provides the content view of the window.
Assigning a view controller to this property (either programmatically
or using Interface Builder) installs the view controller’s view as the
content view of the window. If the window has an existing view
hierarchy, the old views are removed before the new ones are
installed.
addSubview
This method retains view and sets its next responder to the receiver,
which is its new superview.
Views can have only one superview. If view already has a superview and
that view is not the receiver, this method removes the previous
superview before making the receiver its new superview.
So we can say that the main difference is that setting rootViewController destroys all the previous views contained in the UIWindow, and using addSubView: only adds an UIView on top.
self.window.rootViewController = self.tabBarController;
This statement is wrong because window is a container you don't have any root controller there.
SubView:
[self.window addSubview:self.tabBarController.view];
Here you are adding the tabBarController as a subview which will add your windows container. And this is the right way to create the tab bar controller.

UITableView partially hidden by UITabBar

I've got a UITabBarController which contains a UINavigationController. Within the visible UIViewController, I'm creating a UITableView programatically as follows:
self.voucherTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
self.voucherTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
However, the UITabBar is overlapping the UITableView.
When I output the height of the [[UIScreen mainScreen] applicationFrame], it returns 460.00 whereas it should be 367.00.
In Interface Builder, I'm using the 'Simulated Metrics' which automatically sets the height of the view to 367.00.
Is there something I'm missing, no matter what I try I can't see to get the 367.00 height that I need.
As a temp fix, I've set the frame of the UITableView manually, this isn't really ideal so it would be nice to work out why this isn't working:
self.voucherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStylePlain];
You should use self.view.bounds rather than [[UIScreen mainScreen] applicationFrame] as the last one returns you the whole screen frame while self.view.bounds provides you with your view bounds wich seems what you are searching for.
You should add the UINavigationController instance to the UITabBarController and then add a table view controller to the rootViewController property of the UINavigationController instance which should make your life a lot easier.
As a simple example of this, create an empty window-based application (the templates make this a lot more confusing than it really is).
Add your UIViewController/UITableViewController subclasses to the project then use this code as a guide to setting up your project. This code is in your AppDelegate class:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// create our table view controller that will display our store list
StoresViewController *storeListController = [[StoresViewController alloc] init];
// create the navigation controller that will hold our store list and detail view controllers and set the store list as the root view controller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:storeListController];
[navController.tabBarItem setTitle:#"TableView"];
[navController.tabBarItem setImage:[UIImage imageNamed:#"cart.png"]];
// create our browser view controller
BrowserViewController *webBrowserController = [[BrowserViewController alloc] init];
[webBrowserController.tabBarItem setTitle:#"WebView"];
[webBrowserController.tabBarItem setImage:[UIImage imageNamed:#"web.png"]];
// add our view controllers to an array, which will retain them
NSArray *viewControllers = [NSArray arrayWithObjects:navController, webBrowserController, nil];
// release these since they are now retained
[navController release];
[storeListController release];
[webBrowserController release];
// add our array of controllers to the tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:viewControllers];
// set the tab bar controller as our root view controller
[self.window setRootViewController:tabBarController];
// we can release this now since the window is retaining it
[tabBarController release];
[self.window makeKeyAndVisible];
return YES;
}
In the code sample above the BrowserViewController is a subclass of UIViewController and the StoresViewController class is a subclass of UITableViewController. The UITabBarController and UINavigationController instances are created programmatically and added to the window.
By subclassing the UITableViewController class you avoid having to create a UITableView instance programmatically and get most everything you need out of the box.
When you need to push a detail view onto the UINavigationController instance's stack, you just have use something similar to this:
[self.navigationController pushViewController:YourDetailViewControllerInstance animated:YES];
This will add the detail view UIViewController subclass to the UINavigationController instance's view hierarchy for you and animate the transition.
Lots of controllers in this, but it's totally worth it and will avoid a lot of the problems you're experiencing as this method allows the views to manage resizing and take toolbars/navigation bars into account all by themselves.