How to change the navigation bar height - ios

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

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

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

UISearchBar with hidden TableView on NavigationBar

I am trying to create a view with a searchBar on the NavigationBar, I want this searchBar to open a tableView with the search results as soon as typing begins, and hide it once an item is touched. I am new to this platform, so I need just a path to follow, I don't know where to start actually.
According to my comment: Heres a more in depth explanation. Happy coding:
.h
#interface TableViewController : UITableViewController <UISearchBarDelegate>
#property (strong, nonatomic) UISearchBar *searchBar;
#end
.m
- (void) viewDidLoad:(BOOL)animated {
UIView *searchbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; //This adds a container that will hold the search bar.
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
self.searchBar.delegate = self;
[searchbarView addSubview:self.searchBar];
self.tableView.tableHeaderView = searchbarView; //Inserts UIView into the header of self.tableView
[self.tableView setContentOffset:CGPointMake(0, 44)];
}
And thats pretty much it. If you want to customize other things like the keyboard layout and the way text populates and the color and font and placeholder text etc, you can edit it in viewDidLoad or make a subclass
EDIT I have included some example code for customization below:
self.searchBar.keyboardAppearance = UIKeyboardAppearanceDark;
self.searchBar.returnKeyType = UIReturnKeySearch;
self.searchBar.searchBarStyle = UISearchBarStyleProminent;
self.searchBar.barTintColor = [UIColor lightGrayColor];
self.searchBar.placeholder = #"Search for queries here";
self.searchBar.showsCancelButton = YES;
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blueColor],
NSForegroundColorAttributeName,
nil]
forState:UIControlStateNormal];
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
self.searchBar.showsCancelButton = NO;
[self.searchBar resignFirstResponder];
}
You can create a view with a UISearchBar at the top and then a UITableView (initially hidden) and add that to your existing view. Hiding the Navigation bar will given the same appearance as UISearchController. You can then show the table view in the search bar delegates when the user starts searching.

Create UINavigationBar Programmatically

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.

Resources