Im using the following code to customize tabbar.
- (void)viewDidLoad {
[super viewDidLoad];
// The following line centres the UITabBarItem.
[UITabBarItem appearance].titlePositionAdjustment = UIOffsetMake(20, -20);
//the following line make the tabbaritem button with tag 4 to appear as more button
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:4];
}
But the problem is that, the more button is not being shown for the tabbaritem with tag 4. I have five tabs and I want to show more button after three tabs. Whats wrong with my code?
You do not have to manually create a "More..." button.
Simply add all of the View Controllers that you need to the Tab Bar Controller and it will handle the rest.
If you want to create then one of the solution is,
You could make your own More tab and then have it display a navigation controller and table view with the other options.
do it like following way:
You can set the last bar button as a "More" button even when there are only 4 tabs. The initialization code for that tab bar item would be:
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:0];
I hope this will work perfectly..:)
Related
I have a tabbed application written in Objective-C with 3 tabs and I want to add an additional tab that will call a UIAlertController.
However, all tutorials explaining how to add additional tabs to Tab Bar always do so using storyboard, where you first add a viewcontroller and then a segue.
I just want to add a tab bar button and then call the UIAlertController when that button is pressed. Any ideas on how to do this?
I tried adding the code
UIImage* anImage = [UIImage imageNamed:#"beaker.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:#"Home" image:anImage tag:3];
to the viewDidLoad of my TabBarController, but it did not work.
Any suggestions would be greatly appreciated!
AS you can see in the image below, Twitter use different navigation bar color for each view-controller that is pushed.
I've tried almost everything (setbackgroundimage, backgroundcolor, bartintcolor, etc) but nothing seems to work. What i think is that Twitter use custom transition to SIMULATE the push, because, what it seems to me is that every view-controller is presented has his own navigation bar with his own color.
If you want to handle navigationBar with different barTintColors, Code School had a tutorial about it. (iOS App: Creating a Custom Nav Bar)
It could also extended to different backgrounds by using setBackgroundImage:forBarMetrics: method.
There are following four steps:
Handle this in viewWillAppear of the source view controller, hide the navigationBar that navigationController provided and create a new navigationBar to the superView.
- (void)styleNavBar
{
// 1. hide the existing nav bar
[self.navigationController setNavigationBarHidden:YES animated:NO];
// 2. create a new nav bar and style it
UINavigationBar *newNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0)];
[newNavBar setTintColor:[UIColor whiteColor]];
// 3. add a new navigation item w/title to the new nav bar
UINavigationItem *newItem = [[UINavigationItem alloc] init];
newItem.title = #"Source";
[newNavBar setItems:#[newItem]];
// 4. add the nav bar to the main view
[self.view addSubview:newNavBar];
}
Do the same trick in viewWillAppear of the destination view controller, and create a backBarButtonItem as new navigationBar'sleftBarButtonItem.
- (void)styleNavBar
{
[self.navigationController setNavigationBarHidden:YES animated:NO];
UINavigationBar *newNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0)];
[newNavBar setTintColor:[UIColor blueColor]];
UINavigationItem *newItem = [[UINavigationItem alloc] init];
newItem.title = #"Destination";
// BackButtonBlack is an image we created and added to the app’s asset catalog
UIImage *backButtonImage = [UIImage imageNamed:#"BackButtonBlack"];
// any buttons in a navigation bar are UIBarButtonItems, not just regular UIButtons. backTapped: is the method we’ll call when this button is tapped
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(backTapped:)];
// the bar button item is actually set on the navigation item, not the navigation bar itself.
newItem.leftBarButtonItem = backBarButtonItem;
[newNavBar setItems:#[newItem]];
[self.view addSubview:newNavBar];
}
Fill out the backTapped: method so that user is able to tap-to-popover from destination view controller.
- (void)backTapped:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
Considering the swipe-to-pop situation, setting the gesture recognizer’s delegate to self in viewWillAppear of the destination view controller. (The author: The solution here is a bit of a hack.)
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self styleNavBar];
__weak id weakSelf = self;
self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
}
This for swift3 :
You can set the original navbar background with an empty image:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
With this all of the others VC in the view hierarchy will adopt an transparent nav bar.
In the other VC if you want to set an custom image or a custom color simply put a background view in the position of the navbar, this in the view did load method of the particular view controller.
let viewNavBar = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 64))
viewNavBar.backgroundColor = UIColor.white
view.addSubview(viewNavBar)
Whit this you can set any background or image inside the viewNavBar and don't mess with the overall configuration of the navigation bar.
Twitter doesn't change the navigation bar colour. When you're looking at a user's profile, it's a blurred version of the user's cover photo.
As you can see in the transition, the whole user profile view replaces the previous view. The navigation bar doesn't change, it is replaced. They might not even use a UINavigationBar (or at least not the one from the navigation controller).
The "bar" is a custom view that shows the user's cover photo, with the back/search/tweet buttons appearing in their usual positions. The user's cover photo shrinks, blurs and attaches to the top of the screen when you have scrolled down - and at this point, it looks like a normal navigation bar. The user's name and tweet count also scrolls up to the center of the navigation bar at this point.
It's quite intriguing, and their whole view structure for a user's profile probably uses a bunch of tricks. But it's not exactly a simple task to imitate their profile view, and they do much more than just change the tint of their navigation bar. If you just want to do this, Undo's answer works well. However, you may also have to reset the tint colour in your viewWillAppear method (of the old and new views).
Try look up this GitHub repo, I think it could help you achieve that effect https://github.com/kingiol/KDInteractiveNavigationController
I have an app that uses bottom tabs aswell as a side menu, to have the button that initiates the side menu i use the typical three line menu button, to put that there I have a Navigation Bar. With the bar in place there is no way I can get the bar to be on top of the screen. I built it with interface builder, and heres a screenshot. The question is how do i have the navigation bar alone without the other grey bar above it?
The issue you're encountering is due to the fact that you're manually creating a navigation bar for your view controller, instead of using the bar that you get for free by embedding the view controller in a tab bar controller, hence the reason you see two bars. The other answer suggesting hiding the auto-generated navigation bar is not the correct solution. Instead, you should place your menu button and view title in the auto-generated bar instead of manually creating your own (you almost never want to do that, in-fact).
So what you should do instead is set the title property of your view controller to be "News", and the leftBarButtonItem property of the view controller to be your hamburger menu button (an instance of UIBarButtonItem initialized with an image for the icon).
For example (inside your view controller's viewDidLoad method or wherever appropriate):
self.title = #"News";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"menuIcon"] style:UIBarButtonItemStylePlain target:self action:#selector(showSideMenu)];
If you want to remove the topmost navigation bar you need use self.navigationController.navigationBarHidden = YES; for view controllers that used for tabs in UITabBarController:
// StoriesViewController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
I have an app that uses a side menu and has a few main screens that can be accessed from the menu and others that can only be accessed from these screens.
What I want is to have a menu button on the navigation bar that opens the menu and can only be visible on the main screens. On the other screens I want to have a back button, instead of the menu button.
I've already put the menu button like this:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconMenu"] style:UIBarButtonItemStylePlain target:self.revealViewController action:#selector(revealToggle:)];
But I can't figure out how to change it with the back button when I need it.
Assuming than by "main screens" you mean root (first) view controllers in the navigation view controllers corresponding to the selected side menu items, this might be a solution for your problem: you can create a superclass for all your view controllers, say MyBaseViewController and rewrite viewWillAppear: method, that will determine whether it should have a default back button or a "revealSideMenu" button, based on whether it's a "main screen" or not.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self == [self.navigationController.viewControllers firstObject]) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconMenu"] style:UIBarButtonItemStylePlain target:self.revealViewController action:#selector(revealToggle:)];
}
}
you can do that in 2 different ways:
the first is in the viewDidAppear check if the back button is already present (when you push a VC the back button automatically added but is avalaible programmatically only after the viewDidAppear) and then decide to add or not the menu button,
the second is add a parameter to your VC init method like isRoot or hasMenu or whatever you like to name it, and using that flag decide to add the menu or the back button
if you chose to add your own back button you have to call this method in your back selector
[self.navigationController popViewControllerAnimated:YES];
The easiest way, IMO, is just to click on the title bar of the first ViewController and in the Attribute Inspector (⌥+⌘+4) change the Navigation Item info the way you want: Title -> what will show up in the back button* or if you want it to say something other than the Title of the first ViewController or the word "Back" you can just put it in the Back Button field.
*If the Title of the second ViewController is too long for it to fit it will be replaced with the word "Back" (and if the word "Back" doesn't fit it will only have the arrow sign).
I'm not using a navigation controller so I'm just getting a plain rounded rect. I want to get the arrow-esque back button instead, is that possible?
From your comments, you would like to use a navigation controller but don't have room for the navigation bar on your root view controller.
self.navigationController.navigationBarHidden
Will hide or show the navigation bar within a view controller. That should make your life simpler.
It is rather simple, create your custom back button like so:
UIButton *button = /* your custom button, most likely with an image */
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];