I am using Xcode 8.1, on that when I uses TabBarViewController, it doesn't shows separate buttons for each tab, I want to add each Tab's specific image and title.
Following is the screenShot. My Problem is very Simple, I only want to Add Icons and titles to each Tab.
I am beginner.
Any Idea will be Appreciated.
I'm using XCode 8.2.1, but the procedure should be similar to XCode 8.1
Steps to add image into tab
1. In your storyboard, drag a TabBarController from the palette, it comes with 2 default ViewController
Highlight one of the Default ViewController
Select item1 as shown in this image Item1 Sample
Switch to Attributes Inspector Tab as shown in this image Attributes Inspector Sample
Then change the value of Title and Image to what you want.
You're done.
In case You want to know how to manually link those ViewController together.
Drag one TabBarController from the palette.
Remove Both Default ViewController come along with TabBarController.
Drag one ViewController from the palette.
Control+Drag from TabBarController to ViewController.
Select 'view controller' under Relationship Segue.
Then Follow the steps in upper section to add your image and title of your tab bar item
If I understand correctly and you are trying to customize the button icons in the tapbar, then you can do it programmatically using this code for each tapbar button index:
UITabBarController *myTabBar = [[UITabBarController alloc] init];
UITabBarItem *tabButton1 = [myTabBar.items objectAtIndex:0];
[tabButton1 setSelectedImage:[UIImage imageNamed:#"selected_image"]];
tabButton1.selectedImage = [[UIImage imageNamed:#"selected_image"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabButton1.image = [[UIImage imageNamed:#"unselected_image"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabButton1.title = #"Button 1 title";
NSArray *arrayViewControllers = [NSArray viewController1, viewController2, viewController3, nil];
[myTabBar setViewControllers:arrayViewControllers animated:NO];
Alternatively, you can set the image in the storyboard but keep in mind that the image alpha channel will be used to generate the select/unselect color tonality. That sometime lead to strange effects.
Related
I am using the UITabbarview with for item. For every tabbar item I am using the image and title.
Its perfect when I come that TabbarViewcontroller screen and navigate to another to anther UIViewcontroller by push.
But if i navigation to another UIViewController by present then come back to same TabbarviewController screen then title overlap on image of bar item.
like the images
Now I am not able to understand, why this happening and how to resolve this issue.
use this code in your "viewWillAppear" method.
-(void)viewWillAppear:(BOOL)animated
{
[UITabBarItem appearance].titlePositionAdjustment = UIOffsetMake(0, -3);
}
Select tab bar icon and choose image inset all = 0
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..:)
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!
I have the following story board:
As you can see there is a tab bar application with 5 tabs, on the storyboard I've assign the logo for each tab. Now when the user clicks a cell in a particular view I want to change the image of one of the tabs. How can I do this? I don't have an instance of the tab bar view controller or items since storyboards pretty much does all this for me. So my question is what methods do I have to implement to change the image? If I need the tab bar controller how can I get its instance and in which class should I point it to?
Thank you very much,
In any UIViewController class that is part of the Tab Bar hierarchy, all you have to do to get in instance of the tab bar controller is:
//In UIViewController
UITabBarController *tabBarController = self.tabBarController;
You can then change the image as so
//Suppose you want to change the 1st (0th) tab bar image
UITabBarItem * tabItem = [tabBarController.tabBar.items objectAtIndex: 0];
tabItem.image = //whatever image you want to change to
Each UIViewController has a property called tabBarItem which is a UITabBarItem that the tab bar controller uses to set the image representing that controller. You can manipulate that to change the image of the controller in question.
I found that -- at least in Xcode 6.1.1 using Swift -- the direct manipulation of the tabBarItem did not work for me.
However, #borrrden's answer put me on the right track. Apple's documentation for UITabBarController states pretty clearly:
You should never access the tab bar view of a tab bar controller
directly. To configure the tabs of a tab bar controller, you assign
the view controllers that provide the root view for each tab to the
viewControllers property.
...
Tab bar items are configured through their corresponding view
controller. To associate a tab bar item with a view controller, create
a new instance of the UITabBarItem class, configure it appropriately
for the view controller, and assign it to the view controller’s
tabBarItem property.
Therefore, in accordance with that, below is what I came up with that did work for me.
It's written in Swift, and I hope that future readers can translate it accordingly if they need to (I also changed the image names to be super-generic).
I also used UIImage's imageWithRenderingMode method so I could use custom images instead of the shadowy silhouette default images that iOS creates (I would like to credit #NSHeffalump's answer here for that...).
if let viewControllers = tabBarController.viewControllers as? Array<UIViewController> {
var tabBarItemImageNames = ["TabBarItemImage0","TabBarItemImage1","TabBarItemImage2","TabBarItemImage3","TabBarItemImage4"]
var vcIndex = 0
for vc:UIViewController in viewControllers {
let selectedImage = UIImage(named: tabBarItemImageNames[vcIndex])?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let image = UIImage(named: tabBarItemImageNames[vcIndex])?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var tabBarItem = UITabBarItem(title: "", image: image, selectedImage: selectedImage)
vc.tabBarItem = tabBarItem
vcIndex++
}
}
This may not be possible, but I'm hoping someone will have an idea how to do it.
I have an app I'm porting from iPhone only to Universal. On the iPhone, I'm using a Tabbed application. I use three tabs for the normal data to be displayed. I have a forth tab that's only displayed if certain conditions are met. To add the tab, I do:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
UITabBarController *tabController = (UITabBarController *) self.rootViewController;
NSMutableArray* newArray = [NSMutableArray arrayWithArray: tabController.viewControllers];
[newArray addObject: [theStoryboard instantiateViewControllerWithIdentifier: #"AdditionalView-Phone"]];
[tabController setViewControllers:newArray animated:YES];
}
For the iPad, I have enough space on the initial view to display everything from the main three tabs in the iPhone UI. So all I need is one additional (small) view for the "Additional" data. I wanted to do it using a popOver view, so I set up the initial view with a Nav bar and popover button as in the Utility App template. But now I'm stuck. I can't figure out how to create that popover button at run time and make it do the segue to the popOver view properly. I can add the button like this:
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle: #"Modem" style: UIBarButtonItemStylePlain target: self action: #selector(togglePopover:)];
self.navBar.topItem.rightBarButtonItem = flipButton;
but I get an exception: 'NSInternalInconsistencyException', reason: 'UIStoryboardPopoverSegue must be presented from a bar button item or a view.' I'm pretty sure this is because I don't have an anchor set for the popOver segue. The button doesn't exist in the storyboard, so I can't set it there. And I can't seem to find an API to set it at run time.
I also tried creating the button in IB, but not in the view hierarchy, and then just setting the rightBarButtonItem property to my existing button. That also works, but I still can't set that button as the anchor for the popover view. I can set the Navigation Bar as the anchor, but that makes it anchor to the title in the nav bar, which looks silly.
Any ideas?
I had the same problem and solved it by creating a UIBarButtonItem in the Storyboard for the view controller but not part of the view hierarchy.
In IB, Drag a bar button item to the dark bar below the view controller view, drop it next to the "First Responder" and "View Controller" icons. Create a (strong) IBOutlet for it. Then create a popover segue from it to the destination view controller by dragging from the bar button item to the destination. It seems like this is the only way to set it as the anchor. Choosing it as the anchor for an existing segue does not work (looks like an IB bug).
In viewDidLoad you can assign this bar button item to the navigationItem (or where ever you like) and the segue works as expected.
I was curious about this too so I made a quick test project. You're right, there doesn't seem to be a way to configure the popover segue at runtime or add an anchor point to a button that's not in the view hierarchy using Interface Builder.
My solution was to set everything up in IB with the UIBarButtonItem visible and connected to an IBOutlet property, then remove it from the navigation bar in -viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = nil;
}
Then I simply add it back or remove it by tapping another button:
- (IBAction)toggleBarButtonItem:(id)sender
{
UIBarButtonItem *item = (self.navigationItem.rightBarButtonItem == nil) ? self.popoverBarButtonItem : nil;
[self.navigationItem setRightBarButtonItem:item animated:YES];
}
You could conditionally keep or remove the button in -viewDidLoad the same way. The segue remains anchored to the UIBarButtonItem.
I'm gonna try making a dummy view that's the size and shape of the views I want to present the popover from, wire that to the segue popover target, and then move the view to the right position in prepareForSegue:sender:
I'm not sure this is exactly what you want, but this is what I would do. Create a button and set it up with some target/action. Call that target/action method
presentPopover:(UIButton *)sender;
Then in the presentPopover method, say
UIViewController *customAdditionalViewController = [[MySpecialVC alloc] init];
//Configure the customAdditionalViewController
//Present the customAdditionalViewController in a Popover
UIPopoverController *popover = [[UIPopoverController alloc] initWithViewController:customAdditionalViewController];
//Set popover configurations
[popover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:/*whatever you want*/ animated:YES];
That is how I would handle your use case.