How to show UIToolbar in the UIView - iphone

MainViewController have UIToolbar and when button is pressed on the UIToolbar, it displays view i want to show the UIToolbar in all my views.
I set this command in display view action
[self.navigationController setToolbarHidden:NO animated:NO];
but this statement is not helping in displaying UIToolbar in the view
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
[self.view addSubview:viewController.view];
[self.navigationController setToolbarHidden:NO animated:NO];
}
Please suggest how to show UIToolbar in views.
Thanks for help.

Added
[self.view addSubview:toolbar];
This helped in solving the issue.

Related

UIWindow subview disappears after when application becomes active

So, I am trying to achieve having a fixed top banner on Application UIWindow and a working navigation of multiple view controllers. I know there are other ways of performing this but for my project requirements I need to use xibs.
The code below allows to achieve this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.headerView = [[UIImageView alloc] init];
self.headerView.frame = CGRectMake(0.0f, 20.0f, 320.0f, 44.0f);
self.headerView.image = [UIImage imageNamed: #"header-banner-background.png"];
[self.window addSubview:self.headerView];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Then in each ViewController viewWillAppear method I have resized both self.view and self.navigationController.navigationBar.frame in order to show themselves below the banner:
self.navigationController.navigationBar.frame = CGRectMake(0, 64, 320, 44);
self.view.frame = CGRectMake(0, 64, 320, 396);
This is fine, works exactly the way I wanted.
But if the user presses the home button and the application resigns active, when it becomes active again the last viewController seen get's displayed but without the UIWindow banner, and the self.view and navigationBar are displayed on top.
Of course the current UIViewController viewWillAppear method is not called when the application returns active, but even if I put the resize code in another function and call it through a notification it does not work. But when the user presses a bar item and moves in the navigation stack, then everything works again and UIWindow displays the headerView.
Anyone understands why the UIWindow headerView disappears when application returns active?
EDIT 1: Possible solution?
#interface RootViewController ()
{
ViewController *viewController;
UINavigationController *navigationController;
}
#end
#implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
}
return self;
}
- (void)loadView
{
// Implement loadView to create a view hierarchy programmatically, without using a nib.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 88)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[imageView setImage:[UIImage imageNamed:#"header"]];
[view addSubview:imageView];
navigationController.navigationBar.frame = CGRectMake(0, 44, 320, 44);
[view addSubview:navigationController.navigationBar];
self.view = view;
}
EDIT 2: Second possible solution, still not working
- (void)loadView
{
NSLog(#"loadView root");
// Implement loadView to create a view hierarchy programmatically, without using a nib.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 88)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[imageView setImage:[UIImage imageNamed:#"banner"]];
[view addSubview:imageView];
navigationController = [[UINavigationController alloc] init];
navigationController.navigationBar.frame = CGRectMake(0, 43, 320, 44);
[view addSubview:navigationController.navigationBar];
[self addChildViewController:navigationController];
self.view = view;
[self.view setBackgroundColor:[UIColor yellowColor]];
}
This is the result, which seems fine, without the push of the viewController1 (note the yellow background is from the rootViewController):
This instead is the result of adding:
- (void)viewDidLoad
{
viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
[navigationController pushViewController:viewController1 animated:YES];
}
The new view controller is not pushed (the background should be black) and the navigation bar has disappeared.
EDIT 3: Olloqui solution, still not working
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setBackgroundColor:[UIColor redColor]];
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.window addSubview:bannerView];
UIView *navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 416)];
ViewController1 *viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44);
[navigationView addSubview:self.navigationController.navigationBar];
[self.window addSubview:navigationView];
self.window.rootViewController = self.navigationController;
self.window.autoresizesSubviews = YES;
[self.window makeKeyAndVisible];
return YES;
}
Result is I have only the banner, viewController1 is pushed but no navigation bar is displayed. I really need to figure this out, anyone has a different approach?
The window expects to have a root view controlled by its rootViewController property. When returning from the background the window only asks for the rootViewController to restore the appearance of that view controller's view. It doesn't ask all subviews to refresh.
To fix this I would create a new root view controller. This would have the banner and the navigation controller. That navigation controller would then hold your normal view controllers and manage the navigation. The banner will appear properly upon the app returning from the background. Your banner will be stationary while navigation occurs within the navigation controller.
Edit: I whipped up a little example program.
I started with a simple single view app template. I added two view controllers for the content. The just have labels for differentiation, and one has a button that loads the second view.
All the magic happens in the main view controller's viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
OneViewController *oneVC = [[OneViewController alloc] initWithNibName:#"OneViewController" bundle:nil];
self.navController = [[UINavigationController alloc]initWithRootViewController:oneVC];
self.navController.view.frame = CGRectMake(0.0, 64.0, 320.0, 416.0);
[self.view addSubview:self.navController.view];
[self addChildViewController:self.navController];
}
The nib for this view loads the image view for the banner.
The example project in on Bitbucket.
I have never tried it before but I would take a shot to create 2 views in the UIWindow, one for the banner and the other one for the navigation + content. Then, insert the navigationBar view in this second view, that will be sized properly. I am not sure if the navigation is going to try to take the full window are, but resizing the navigation controller on each viewWillAppear doesnt look a good solution to me. Try it and comment the result!
I think you can try to workaround this problem by adding the banner view to window.rootViewController.view like this:
[window.rootViewController.view addSubview:bannerView]
Ok, I really needed to solve this ASAP, therefore I opted for a solution I was lucky enough to find here. I still have to solve the management of the UITabBarController added on bottom but the rest seems to be working fine. Thanks everyone for help and additional hints.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setBackgroundColor:[UIColor redColor]];
// Create a view for the banner add the banner to this view
UIImageView *bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
bannerImageView.image = [UIImage imageNamed:#"banner"];
// put this banner above the nav bar
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
[bannerView addSubview:bannerImageView];
//add the tabBarController as a subview of the view
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController.view addSubview:bannerView];
// Move the root view to show status bar & banner
tabBarController.view.frame = CGRectMake(0, 20+44, 320, 480-20-44);
//add the modified logo view to window
[self.window addSubview:tabBarController.view];
ViewController1 *viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, nil];
//tabBarController.tabBar.hidden = YES;
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}

Gap between statusbar and view

Dealing with this issue for last so many days when button is pressed on the Mainviewcontroller it plays audiofile as well as displays View with UIToolbar at the bottom of the View.
UIView has UIImage and UIText
My issue is that when View is loaded it shows gap between status bar and the loaded view and when infobutton is pressed from the UIToolbar on the view it presents Modalviewcontroller and when this Modalviewcontroller is dismissed back to the View it shows same gap between the status bar and the View.
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
self.view.superview.frame = CGRectMake(0, 0, 320, 480);
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
}
Please anyone knows how to fix this issue. Appreciate your help.
Thanks
After struggling for 2 days got the solution for my problem. It was very simple.
I made these changes
- (void)displayviewsAction:(id)sender
{
//self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
//self.view.superview.frame = CGRectMake(0, 0, 320, 480);
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
}
Now there is no gap. It is working fine.

Add UIToolbar to UIViewController

How to add UIToolbar to the below UIViewController.
PageOneViewController *viewController = [[PageOneViewController alloc] init];
[self presentModalViewController:viewController animated:YES];
[viewController release];
This UIViewController is the view of the mainviewcontroller and UIToolbar is in the mainviewcontroller. So basically when this view is loaded i want UIToolbar to load as well.
Can someone give me an idea
Just add the UIToolbar as a subview of the PageOneViewController:
UIToolbar *toolbar = [[UIToolbar alloc] init];
[viewController addSubview:toolbar];
[toolbar release];
toolbar.frame = // Set this to position the toolbar correctly
[self presentModalViewController:viewController animated:YES];

How to flip vertically UIViewController which is presented modally

How can I vertically flip a UIViewController which is presented modally along with a UINavigationController and Done Button.
This is how I am presenting the UIViewController modally:
- (void)modalViewAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds] autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_viewController = [[ModalViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
[_viewController.navigationItem setLeftBarButtonItem:button animated:YES];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[self.navigationController presentModalViewController:self.viewController animated:YES];
[self.view addSubview:navigationController.view];
[navigationController release];
}
How can I flip this UIViewController whilst presenting it modally?
Thanks for your help.
You can specify the desired animation technique by setting the modalTransitionStyle property (before presenting the view controller).
Unfortunately this property can be set only to:
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl
So vertical flip it is not supported.
Take a look at UIModalTransitionStyle reference:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

How to resize to fullscreen when touched the view that in TabBarController based app?

Here're some view controller I have: tabBarController, navigationBarController, viewController, and imageView.
The hierarchy is that: tabBarController is the root controller, it includes navigationBarController. And the viewController(which is include the imageView) is pushed in by navigationBarController.
So the question is, when I touched the imageView, how to resize it to fullscreen with smooth animation. Note, I don't want to hide NavigationBar & TabBar.
I tried it by setFrame, however, it is covered by navigationBarController and tabBarController.
I tried presentModalViewController: but it lose continuity.
I also tried to reset rootViewController to viewController, but the resulted with a broken animation. :(
Thanks for any suggestion!
Test code
UIViewController * viewController = [[UIViewController alloc] init];
[viewController.view setFrame:CGRectMake(0.0f, 0.0, 320.0f, 480.0f)];
[viewController.view setBackgroundColor:[UIColor redColor]];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[imageView setBackgroundColor:[UIColor yellowColor]];
[imageView setUserInteractionEnabled:YES];
[viewController.view addSubview:imageView];
[imageView release];
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[viewController release];
UIViewController * anotherTabViewController = [[UIViewController alloc] init];
[anotherTabViewController.view setFrame:CGRectMake(0.0f, 0.0, 320.0f, 480.0f)];
[anotherTabViewController.view setBackgroundColor:[UIColor blueColor]];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, anotherTabViewController, nil];
[navigationController release];
[anotherTabViewController release];
[self.window setRootViewController:tabBarController];
Here I write a simple code for testing. Now don't care about the touch action. I wonder how to set the imageView to totally fullscreen(width:320, height:480). But it's clipped by navigation bar and tab bar. I know the frame is relative to its super view. How to deal with this issue?
try to add img to you window, when you need to resize it to full screen:
UIImageView *img; //create new image
img = yourImageView;
[img setOrigin:CGPointMake(yourImageView.frame.origin.x, yourImageView.frame.origin.y+90)];
//window size is bigger, so to set an image frame visionary at same place, you need to set origin point lower
MOEAppDelegate *sharedAppDelegate = (MOEAppDelegate *)[[UIApplication sharedApplication] delegate];
[sharedAppDelegate.window addSubview:img];
[UIView animateWithDuration:1
animations:^{
[img setFrame:CGRectMake(0,
0,
320,
480)];
}];
Note, that your self.view's frame automatically resized when you adding tabBar or navigationBar, and in -(void)viewDidLoad it is NOT resized jet!