Create UINavigationBar Programmatically - ios

How come this code is not producing a UINavigationBar on my UIView?
//create the view
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor whiteColor];
//everything for tabbar
UINavigationBar *navBar = [[UINavigationBar alloc] init];
//add the components to the view
[view addSubview: navBar];
//show the view
self.view = view;
I see the white view but no NavBar. Any help? What am I doing wrong?

Set the frame of navigation bar and add it as a subview of the view for it to be seen.Set a frame for any view to be positioned in its super view's coordinate system.
[navBar setFrame:CGRectMake(0,0,CGRectGetWidth(view.frame),44)];
[self.view addSubview:navBar];
Also its a navigation bar, I do not know what tab bar you are willing to see from this code you posted.

Related

Customize status bar background for UINavigationController

I want a black status bar with white text.
General approach I used is having light style status bar to create a custom black view and to add it to the top of the view controller
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
view.backgroundColor = [UIColor blackColor];
[self.view addSubview:view];
If the view controller is embedded in a navigation controller I do
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
view.backgroundColor = [UIColor blackColor];
[self.navigationController.navigationBar addSubview:view];
However this seems to be not a very elegant solution, maybe there are better approaches?
One way that you can do it by adding view on keywindow. It doesn't matter whether your app is running under UINavigationController or under UIViewController. I have did this by making extension of UIStatusBarStyle as below.
extension UIStatusBarStyle {
static func setbackground() {
let view = UIView.init(frame: UIApplication.shared.statusBarFrame)
view.backgroundColor = UIColor.black
UIApplication.shared.keyWindow?.addSubview(view)
}
}
Now call this method in appdelegate from method didFinishLaunchingWithOptions.
UIStatusBarStyle.setbackground()
Also, make sure that in your info.plist file you have set
View controller-based status bar appearance = NO
Status bar style = Opaque black style

Resize view to account for toolbar

If I create a basic ViewController and add an opaque UIToolbar to the view, like so:
UIViewController* viewController = [[UIViewController alloc] init];
[viewController.view setBackgroundColor:[UIColor blueColor]]; // To make it easy to see
self.window.rootViewController = viewController;
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 49, viewController.view.frame.size.width, 49)]; // At bottom. Height of 49
[toolbar setBarStyle:UIBarStyleBlack]; // Opaque and easy to see
[self.window.rootViewController.view addSubview:toolbar]; // Add toolbar to view controller
The view hierarchy ends up like below:
How do I make the View controller resize it's main area (the blue part) so it doesn't extend behind the UIToolbar?
Try to add this line of code
viewController.edgesForExtendedLayout = UIRectEdgeNone;
this will make so view controller's edges are not extended in any direction.
Or you can add this if you want to extend top, left and right, but not bottom.
viewController.edgesForExtendedLayout = UIRectEdgeTop, UIRectEdgeLeft, UIRectEdgeRight;
Try like this
UIViewController* viewController = [[UIViewController alloc] init];
[viewController.view setBackgroundColor:[UIColor blueColor]]; // To make it easy to see
self.window.rootViewController = viewController;
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 49, viewController.view.frame.size.width, 49)]; // At bottom. Height of 49
[toolbar setBarStyle:UIBarStyleBlack]; // Opaque and easy to see
SecondViewController* secondChildVC = [self.storyboard instantiateViewControllerWithIdentifier:#"someId"];
[secondChildVC.view setBackgroundColor:[UIColor redColor]];
[viewController addChildViewController:secondChildVC];
[secondChildVC didMoveToParentViewController:viewController];
secondChildVC.view.frame = CGRectMake(0,0,viewController.view.frame.size.width,viewController.view.frame.size.height - 49);
[viewController.view addSubview:secondChildVC.view];
[self.window.rootViewController.view addSubview:toolbar];

How to change the navigation bar height

I saw a solution to change the height of navigation bar. But nothing worked for me. Now my application has one view controller connected with a navigation controller. I have not yet implemented any other code in my project. Before starting my project I need to change my height of my navigation bar.
edited:
.h:
- (CGSize)sizeThatFits:(CGSize)size ;
.m:
#implementation UINavigationBar (customNav)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(370,40);
return newSize;
}
#end
UIView *NavView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , [UIScreen mainScreen].bounds.size.width , 44)];
NavView.backgroundColor = [UIColor clearColor];
NavView.userInteractionEnabled = YES;
[self.navigationController.navigationBar addSubview:NavView];
UIButton *DateBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 90, 30)];
DateBtn.backgroundColor = [UIColor clearColor];
[DateBtn setTitle:#"Jan 05" forState:UIControlStateNormal];
DateBtn.titleLabel.font = [UIFont fontWithName:BrushScriptStd size:18];
[DateBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[NavView addSubview:DateBtn];
You can not change UINavigation height but you can add you custom view on UINavigation bar
Very simple solution to this problem is you can create view from StoryBoard or from Code.
And add the view to your viewController.
You have to disable default navigation Bar from you project. You can do it by Storyboard also. Select your navigation bar and change the Status Bar to none:
And If you want to do via code then in your didFinishLaunching wirte this code:
UIStoryboard *tStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
RootVC* RootVCObj = [tStoryBoard instantiateViewControllerWithIdentifier:#"RootVC"];
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:RootVCObj];
navC.navigationBarHidden = YES;
After this add as many as Buttons to your view
Main cons of this is that you have to make custom view for all Screens you currently have

Do I need to embed a UINavigationController & Programmatically add UINavigationBar?

I don't see all of my navigation bar.
There is only a outline of where a nav bar would be.
I set the navigationBar Color and tint colors so as to define this nav bar even if it had no items.
I used Storyboards to make the currently functional UI.
My Story Board UI includes Delegates,Custom Collection View Cells,Custom Table View Cells,And Animations.
By the end of my app I wanted to add nav bars programmatically to each of my ViewControllers. (see code)
I have used this code in 6 other classes. And I Have 6 Perfectly Flawless navigation bars in those ViewControllers.
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc]
initWithTarget:self
action:#selector(titleGestureLabelMethod)];
UILabel * titleView = [UILabel new];
titleView.text = #"Title";
titleView.textColor = [UIColor greenColor];
[titleView sizeToFit];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:tapGesture];
self.navigationItem.titleView = titleView;
[self.view addSubview:titleView];
[self.navigationController.navigationBar setTranslucent:NO];
UIBarButtonItem *lButton = [[UIBarButtonItem alloc] initWithTitle:#"L"
style:UIBarButtonItemStylePlain
target:sel
action:#selector(lButtonMethod)];
[self.navigationItem setLeftBarButtonItem:lButton animated:YES];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithTitle:#"R"
style:UIBarButtonItemStylePlain
target:self
action:#selector(rButtonMethod)];
[self.navigationItem setRightBarButtonItem:rButton animated:YES];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.view addSubview:testTwoNavBar];
}
You need to embed your view controller into a navigation controller. If you're using Interface Builder, from your storyboard select the view controller. In the menu select editor and choose embed in navigation controller. It's hard to understand what you really need though from this question.

How to add a custom UIView to Navigation Bar in iOS7+ like this

Is there a way to insert any UIView in NavigationBar like the image below?
Is this not working?
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
In the code below, I embed a view into the navbar, where my embedded view draws the background and the new buttons. The code also deals with relaying out the view when the device is rotated.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (view.superview == nil) {
[self.navigationBar addSubview:view];
[self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
}
CGRect frame = self.navigationBar.frame;
frame.size.height = MY_NAVBAR_HEIGHT;
self.navigationBar.frame = frame;
view.frame = self.navigationBar.bounds;
}
An alternative solution is to use a regular UIView as a NavBar, and by saying that what I mean is you have to set the UIView in navbar place and hide the NavigationController bar in the main VC and show it in the next one read this article Here https://medium.com/me/stats/post/54bc5fc56d6c

Resources