How to get transparent navigation bar in root viewcontroller using UIappearance - ios7

In my app's delegate, I specify a transparent tool bar with (as suggested in an answer to question 18969248):-
The code is:
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
navigationBarAppearance.backgroundColor = [UIColor clearColor];
[navigationBarAppearance setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
navigationBarAppearance.shadowImage = [[UIImage alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
This works for all view controllers pushed on to the navigation controller's stack, but not for the root view controller (which is loaded from a NIB).
How can I get transparency in the root view controller's navigation bar?

Maybe you should load your RootViewController programmatically through your AppDelegate this way, if you're using storyboard :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"yourStoryboard"
bundle: nil];
YourCustomRootViewController *customRootVC = (YourCustomRootViewController*) [mainStoryboard instantiateViewControllerWithIdentifier:#"firstAddProductViewController"];
// If you're not using storyboard, simply instantiate it this way
YourCustomRootViewController *customRootVC = [[YourCustomRootViewController alloc] initWithNibName:#"yourNib" bundle:nil];
/* In here, you want to add the code relative to the navigation bar of your rootVC */
[self.window setRootViewController:customRootVC];
}

Related

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

Modifying UINavigation controller programatically in Xcode5

I have a programmatically generated UINavigationController applied to each of my ViewControllers but this UINavigationController is blank. I need to add some text, an image (if possible) and two buttons.
I have managed to do something similar with a very basic app that only has one ViewController and UINavigationController created in the normal manner but do not know what to do in regards to a programmatically generated one.
I am using storyboards in XCode5/iOS7.
Here is the code that generates the UINavigationController and launches the first ViewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
MainViewController* mainVC = [mainStoryboard instantiateInitialViewController];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window setRootViewController:navVC];
[_window makeKeyAndVisible];
return YES;
}
I have tried things that I thought would be obvious like:
navVC.navigationBar.backgroundColor = [UIColor redColor];
To change the colour or:
[navVC setTitle:#"Test title"];
But nothing changes, the only way I could get the title to change was to change the title of mainVC
[mainVC setTitle:#"Test title"];
Changing the title in the MainViewController.m file itself does nothing.
So how do I modify the UINavigationController (navVC)?
Thanks,
You should be able to set an image to your navigation bar in this way:
UINavigationBar *navBar = navVC.navigationBar;
UIImage *image = [UIImage imageNamed:#"YourNavBarImg.png"];
[navBar setBackgroundImage:image];
and this to change the nav bar tint color:
[navVC.navigationBar setTranslucent:NO];
[navVC.navigationBar setBarTintColor:[UIColor redColor]];
about buttons (UIBarButtonItem) into nav bar:
// where "self" is your current viewcontroller
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:button1,button2, nil];

Second UIScreen causing problems with UIStoryboards

In my app delegate i set up another screen (for airPlay).
When i create a second window and assign the Second screen to it and run the app the TableView in my storyboard turns to a black colour. Its as if the tableView is not rendering correctly. I have isolated the problem to :
self.HDTVwindow.screen=[[UIScreen screens] objectAtIndex:1];
where the HDTVWindow is the second window for my airplay app. When i comment out this code the storyboard runs fine and my UITableView is nice and white. Am i confusing the storyboard by making two UIWindows in the appDelegate, even though they are assigned to different screens??
For my appDelegate see below.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[UIScreen screens] count]>1)
{
CGRect frame = [[UIScreen screens]objectAtIndex:1].bounds;
self.HDTVwindow = [[UIWindow alloc] initWithFrame:frame];
UIImage* astonLogo=[UIImage imageNamed:#"AstonUni720p.png"];
UIImageView* astonLogoView=[[UIImageView alloc] initWithImage:astonLogo];
astonLogoView.frame=frame;
[self.HDTVwindow addSubview:astonLogoView];
self.HDTVwindow.hidden = NO;
self.HDTVwindow.screen=[[UIScreen screens] objectAtIndex:1];//PROBLEM!!
}
// Set Up Storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *mainViewController = [storyboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
The problem disapears when ran with the iPad and an apple TV, it seems there might a bug in the simulator. In future i shall always run my code on real ios devices.

Navigation controller and no storyboards

I am writing an app in Objective C. For this project I cannot use ARC or storyboards. It should have 6 views. Firstly, the navigation controller shall keep table view (it will contain some data passed in from an array). In the toolbar on the top once the item has been pressed the app should move to second view.
I know that I need to code that in my delegate file, but I a not quite sure what needs to be included. Those were the requirements.
In fact when I run my app it does not show that navigation controller which got table view as my entry view. In the settings I cannot find a checkbox in order to select it and make it an entry view.
Any suggestions?
Best regards
Best way to use UITabBarController
Firsts Create all object of UIViewController and UINavigationController in AppDelegate.h file and use following method of AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds ]];
self.viewCon=[[ViewController alloc] init];
self.navCon=[[UINavigationController alloc] initWithRootViewController:self.viewCon];
self.navCon.navigationBar.tintColor=[UIColor blackColor];
self.viewCon.title=#"First View";
self.fView=[[FirstViewController alloc] init];
self.FnavCon=[[UINavigationController alloc] initWithRootViewController:self.fView];
self.FnavCon.navigationBar.tintColor=[UIColor blackColor];
self.fView.title=#"Secound View";
self.sView=[[SecoundViewController alloc] init];
self.SnavCon=[[UINavigationController alloc] initWithRootViewController:self.sView];
self.SnavCon.navigationBar.tintColor=[UIColor blackColor];
self.sView.title=#"Third View";
.
.
// create UIViewController and UINavigationController As you need
.
.
.
UIImage *img1=[UIImage imageNamed:#"Australia.gif"];
self.tbItem1=[[UITabBarItem alloc] initWithTitle:#"First Page" image:img1 tag:1];
self.viewCon.tabBarItem=self.tbItem1;
UIImage *img2=[UIImage imageNamed:#"Cameroon.gif"];
self.tbItem2=[[UITabBarItem alloc] initWithTitle:#"Secound Page" image:img2 tag:2];
self.fView.tabBarItem=self.tbItem2;
UIImage *img3=[UIImage imageNamed:#"Canada.png"];
self.tbItem3=[[UITabBarItem alloc] initWithTitle:#"Third Page" image:img3 tag:3];
self.sView.tabBarItem=self.tbItem3;
NSMutableArray *viewArr=[[NSMutableArray alloc] init];
[viewArr addObject:self.navCon];
[viewArr addObject:self.FnavCon];
[viewArr addObject:self.SnavCon];
self.tbCon=[[UITabBarController alloc] init];
self.tbCon.viewControllers=viewArr;
[self.window addSubview:tbCon.view];
[self.window makeKeyAndVisible];
return YES;
}

How to present a (no nib) ViewController

I've created a ViewController (without nib files), so how do I load this viewController in the app delegtate? my current code is:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
viewController = [[MyCustomViewController alloc] init...]; // I use a custom init method
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
And how can I use the presentModalViewController method to show new view controllers of the same "CustomViewController" class?
THANKS for the help!
NOTE: By the way, I do see that this code does call my initialization method, and my ViewDidLoad method in my customViewController is being called however the screen is still black....
It looks like your properties are not set up correctly.
Check this line:
viewController = [[MyCustomViewController alloc] init...]; // I use a custom init method
self.window.rootViewController = self.viewController;
check that self.viewController is okay, and there's nothing stopping you from just doing:
self.window.rootViewController = viewController;
Whoops, I was just missing this code in my viewDidLoad Method:
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view.backgroundColor = [UIColor whiteColor];
So everything was working, but the screen by default is black and without a frame.
I had to do just that in one of the apps I'm working on. Try typecasting your view controller
CDViewController *viewController = (CDViewController*)self.window.rootViewController;
viewController.managedObjectContext = self.managedObjectContext;
viewController.managedObjectModel = self.managedObjectModel;
// Override point for customization after application launch.
return YES;