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

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
}

Related

DidSelect TabBar Item Not Being Called

So I have a UITabBarController with two View Controllers Embedded into it. I implemented the did select tab bar method where when the user selects a tab, it passes a value into that controller. However when the tabBarController loads for the first time, the did select method is not called even though I have
self.selectedIndex = 0
Which selects the first index. Basically I am just trying to automatically select the first tab Bar Item when the view loads, and have it call the didSelectTabBarItem method
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
}
The question is similar to this one.
tabBarController didSelect does not get called
optional func tabBarController(_ tabBarController: UITabBarController,
didSelect viewController: UIViewController)
In iOS v3.0 and later, the tab bar controller calls this method regardless of whether the selected view controller changed. In addition, it is called only in response to user taps in the tab bar and is not called when your code changes the tab bar contents programmatically.
Copy this in your code
class HomeTabBarVC: UITabBarController {
var isIpad = false
let button = UIButton.init(type: .custom)
var index = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
APPDELEGATE.tabBar = self
}
}
extension HomeTabBarVC : UITabBarControllerDelegate {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the last selected index is : \(selectedIndex)")
APPDELEGATE.tabBarLastSelectedIndex = selectedIndex
print("the current selected index is : \(String(describing: tabBar.items?.index(of: item)))")
APPDELEGATE.tabBarCurrentSelectedIndex = tabBar.items?.index(of: item) ?? 0
}
}
In appdelegate declare this variable,
var tabBar : UITabBarController?

How to show six TabBarItem in UITabBar

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
}
}

Cannot select specific Tab Index to show in Tab Bar Controller, always shows 0th index

I have a tab bar controller with four tabs. I want to show the first item at the beginning. With a button click from the first item (view), when it is clicked, I want to show the second tab. How can I do that?
I created a custom tabbarController class and tried to give tabbarindex like below. I checked at the beginning without a button click but it didn't work. It always loads the first tab bar item.
class HTabViewController: UITabBarController, UITabBarControllerDelegate {
var controllerArray : [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.selectedIndex = 2
self.tabBar.tintColor = UIColor.red
// defineViewController()
}
}
Note : can we show specific tab item with a button click?
Since self is the UITabBarController, you need to set the selectedIndex on self, not self.tabBarController.
override func viewDidLoad() {
super.viewDidLoad()
selectedIndex = 2
tabBar.tintColor = UIColor.red
}
job done, add self.selectedIndex = needed_index at viewDidLoad() method

Use tab bar button to reset view?

I have a tab controller which is displaying my home screen. In my home screen, there are optional filters for my collection view there. I would like the filters to be reset if the user taps on the same tab bar button. Currently, tapping on the tab bar button doesn't do anything if you are already in the same view of the tab bar button. How can I execute code if the tab bar button was tapped a second time?
Try this method in a UITabBarController subclass or put tabbar delegate in your controller.
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
var isStatusbarWhite = false
for xx in (tabBar.items as! [UITabBarItem]){
for var i:Int = 0 ; i < self.tabBar.items?.count ; ++i {
let xx = tabBar.items![i] as! UITabBarItem
if (xx == item){
if tabBarController.selectedIndex == i {
//YOUR CODE HERE SAME TAB SELECTED
}
}
}
}

change storyboard tab bar icon using swift

I've created a tab bar controller and embedded several view controllers in it. I then try to set a specific tab bar item in a corresponding swift file for that view. Problem is that I am changing the item by overriding the "viewDidLoad" function. So it only updates after the user has touched that item. What be a better approach to changing storyboard tab bar items using swift?
create a subclass of UITabBarController
set this custom class of your TabBarController
now you override UITabBarController ViewDidLoad Method
there you can access all the TabItems and change their text/images
before a ViewController get loaded.
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let allItems:[AnyObject] = self.tabBar.items!
var item1:UITabBarItem = allItems[0] as! UITabBarItem
var item2:UITabBarItem = allItems[1] as! UITabBarItem
item1.image = UIImage(named: "menu#2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
item2.image = UIImage(named: "play#2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
}
}
in custom class the code should be like as above...
you can set selectedState Image as well....
here is the result..

Resources