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?
Related
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
}
}
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.
I want to know when the tab in the tab bar changes, so that i can report it to Firebase Analytics. How do i do this.
I tried this
override func viewDidAppear(_ animated: Bool) {
Analytics.logEvent("projects_open", parameters: [:])
}
But i have a feeling that what would also run when i go back to it from another ViewController. I need something that can detect when a tab is opened, not when it becomes visible.
Is there another func that works for this?
Swift 3.0
Use this two delegate methods, and don't forget to assign delegate to self.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
//MARK: - UITabBarControllerDelegate
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
//MARk: - UITabBarDelegate
}
There is a delegate function on UITabbarController for detecting that a tab was selected:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
You can also access the selected index like this:
tabController.selectedIndex
If you use only tabBar.in viewDidLoad set tabBar delegate to self and
override func tabBar(_ tabBar: UITabBar, didSelect item:UITabBarItem)
{
//MARk: - UITabBarDelegate
if(tabBar.selectedIndex == 0) {
//Do something
}
else if(tabBar.selectedIndex == 1) {
//Do something.
}
}
and if you use tabBarController use this method.And mark delegate as self
func tabBarController(_ tabBarController: UITabBarController,
didSelect viewController: UIViewController) {
}
Very important Note:
If you want to save which tabBar was previously selected you have to save it on your way.Either use flag or NSUserDefaults according to your wish. The reason i mentioned this because i needed to check which tab has been selected right now in View in one of my project.
Swift 5
Easy way just click on link StakOverFlow screen will open
https://stackoverflow.com/a/60539396/6881070
I am using Tab Bar i.e. Bottom bar and have 5 tab bar items, i want to assign a method to each, so that i can navigate to other views on tab bar item click.
I've been looking for some clues, but couldn't make it.
Use UITabBarDelegate.
Implement your class and inherit the protocol by adding after your class definition
#interface MyViewController : UIViewController<UITabBarDelegate>
and then use method tabBar:didSelectItem: in that class
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// Do Stuff!
// if(item.title == #"First") {...}
}
May be it will help you
In Swift
Implement UITabBarDelegate and use method didSelect
class MyViewController: UIViewController, UITabBarDelegate {
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
}
}
Also create IBOutlet to toolbar
toolbar.delegate = self
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