How to show six TabBarItem in UITabBar - ios

Can I show six TabBarItem in UITabBar, I try to resize TabBarItem but it can't.
uitabbar

With default UITabBarController you can not because it will add More tab like shown below:
and you need to click on More tab to show another options.
But you can use third party library like AZTabBarController which will populate all six options as shown below:
More libraries for tab bar can found HERE.

This is against "Human Interface Guidelines", but it is possible to have as many TabBarItems as you want using tabBar without tabBarController. You may lay out tabBar and items in storyboard. To respond to taps conform to UITabBarDelegate and implement at least didSelectItem.
import UIKit
class AdminViewController: UIViewController, UITabBarDelegate {
#IBOutlet weak var tabbar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
tabbar.delegate = self;
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
print(item.tag);
switch item.tag { // switching by tag is not required, just an option
case 1:
// segue or push or present something
case 2:
// segue or push or present something
default:
break
}
}

Related

Make some but not all tabs in an UITabViewController reorderbable

Is there a possibility to make some but not all tabs re-orderable in an UITabbarController in iOS 12+?
I looked for a related UITabbarDelegate or an option in the storyboard but this was not a success.
MY idea was to use delegate function func tabBar(_ tabBar: UITabBar, willBeginCustomizing items: [UITabBarItem]) and just call the super implementation with the sortable items. But this has no effect.
class TabbarController: UITabBarController
{
override func tabBar(_ tabBar: UITabBar, willBeginCustomizing items: [UITabBarItem]) {
super.tabBar(tabBar, willBeginCustomizing: [items.first!])
}
}
Taken from here.
By default, the user is allowed to rearrange all items on the tab bar. If you do not want the user to modify some items, though, you can remove the appropriate view controllers from the array in the customizableViewControllers property.
Is it what you're looking for?

Action not working if UITabBar has more than 5 items

I want to perform an action when the user clicked on some tabs in UITabBar without opening another view. For example, setting tab or share.
Here is what I did :
class ViewTabBarController: UITabBarController,UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Do any additional setup after loading the view.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("do something")
}
}
This code works fine if the UITabBar has just 5 items.
But the problem here is that if UITabBar has more than 5 items those that are under the "More Tab" did not call the tabBar() function when clicked.
From the documentation description of the didSelect method of UITabBarControllerDelegate:
Tells the delegate that the user selected an item in the tab bar.
What that means is that the method is called when the user taps on one of the buttons in the bar. When you have a 'more' button then that is the button in the tab bar so tapping 'more' fires that method.
The view controllers in the 'more' section are actually processed in a different way involving the use of a UINavigationController. With these views they don't have a button in the tab bar and therefore this even is not fired.

swift check which tab bar item was clicked

I am using a tab bar controller and I wonder if there is a way to check which tab is being clicked?
If the user clicks on the "account" tab and is not logged in I want to redirect to a full screen modal login screen instead of the account VC.
You can do it in your custom UITabBarController or somewhere, and override the 'didSelectItem' function.
import UIKit
class TabbarViewController: UITabBarController {
override func viewDidLoad() {
}
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
print("Selected Index :\(self.selectedIndex)");
}
}
In the scenario you outlined, I would check to see if current user is logged in or not, and if not segue to the appropriate screen of your application.
UITabBarDelegate's didSelectItem
The options that others have provided are fine, but I wanted to let you know of another way. In the viewWillAppear, viewDidAppear, or viewDidLoad functions, you can call what you need to segue to a login ViewController

Get the title of the tab selected in UITabBarController at application launch

I am trying to get the tab title at application launch.
I can do this to read the tabBarItem.title when the user changes tabs:
func tabBarController(
tabBarController: UITabBarController,
didSelectViewController viewController: UIViewController) {
UserActivity.trackScreen(name: viewController.tabBarItem.title)
}
This method does not fire for the initial selection. I tried this approach in UITabVarController's viewDidLoad method.
override func viewDidLoad() {
super.viewDidLoad()
UserActivity.trackScreen(name: self.selectedItem.title) // I think this is not set yet, it is nil.
}
This does not work.
How do I get the selected tab bar item, or the tab bar item that will be selected, on app launch?
Doing an action on the "selected" tab is a special case for the first launch, because the delegate method didSelectViewController does not fire.
If (and this might be a big "if") you can assume the first tab is the one that is selected on app launch, this code will work for handling the first-launch case:
if let vcs = self.viewControllers {
var firstVC = vcs[0] as UIViewController
UserActivity.trackScreen(name: firstVC.tabBarItem.title)
}
This works for me. Open to better answers.

Can't set default button on tab bar in view controller

I have a View Controller with a UITabBar on the bottom. This tab bar performs a different function depending on which tab is selected. I would like to have one of the buttons selected when the view controller loads. I can't find a solution that doesn't involve a tabbarviewcontroller
class ViewController: UIViewController, UITabBarDelegate {
#IBOutlet var tabBar : UITabBar
//In view controller declare a UITabBar as an Outlet
override func viewDidLoad() {
super.viewDidLoad()
tabBar.selectedItem = tabBar.items[0] as UITabBarItem;// here you
can load the default button option which you want to load
}
func tabBar(tabBar: UITabBar!, didSelectItem item: UITabBarItem!) {
var tabBarValue = 0;
switch item.tag {
case 0:
tabBarValue = // assign value
break
case 1:
tabBarValue = // assign value
break
default:
break
}}
Pl. refer to the below url also, where they try to load different webview for the selected tab, Use a UITabBar without UITabBarController to control a UIWebView
This was the solution that worked out for me.
for i in tabBar.items as [UITabBarItem] {
if i.tag == 0 {
tabBar.selectedItem = i
}

Resources