How can I programmatically set selected tab of UITabBarController while also triggering shouldSelectViewController in UITabBarControllerDelegate
If you have implemented - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
in your tabBarController's delegate than you can call it manually.
[self.tabBarController.delegate tabBarController:self.tabBarController shouldSelectViewController:[[tabBar viewControllers] objectAtIndex:2]];
[self.tabBarController setSelectedIndex:2];
Hope this helps.
shouldSelect method of UITabBarControllerDelegate never invoked
Your problem is that you are setting the wrong delegate. Update viewDidLoad
to:
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self // or just "delegate = self"
}
The idea is that you want this tab controller to be its own delegate.
Overriding TabBar selectedIndex
As apple documentation says about selectedIndex
:
This property nominally represents an index into the array of the
viewControllers property.
So it's computed property which returns firstIndex of selectedViewController from viewControllers.
And on setting it changes the selectedViewController.
Use some other UITabBarController property instead. F.e:
override var selectedViewController: UIViewController? {
didSet {
print(selectedIndex)
refreshTabBar()
}
}
how to get the event that switch tab menu on iphone
If you are using storyboard, do this
in didFinishLaunchingWithOptions
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setDelegate:self];
Also in AppDelegate, keep <UITabBarControllerDelegate>
And then
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//Write your code here
}
getting current active viewController in UITabBarControllerDelegate
You need to make a code in should select instead of didselect. As it is unable to find the previous controller after selection. below is the example code for it.
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.selectedViewController == viewController {
print("Same viewcontroller")
}
return true
}
iPhone: How to switch tabs with an animation?
Update 04/2016: Justed wanted to update this to say thank you to everyone for all the votes. Please also note that this was originally written way back when ... before ARC, before constraints, before ... a lot of stuff! So please take this into account when deciding whether to use these techniques. There may be more modern approaches. Oh, and if you find one. Please add a response so everyone can see. Thanks.
Some time later ...
After much research I came up with two working solutions. Both of these worked and did the animation between tabs.
Solution 1: transition from view (simple)
This is the easiest and makes use of a predefined UIView transition method. With this solution we don't need to manage the views because the method does the work for us.
// Get views. controllerIndex is passed in as the controller we want to go to.
UIView * fromView = tabBarController.selectedViewController.view;
UIView * toView = [[tabBarController.viewControllers objectAtIndex:controllerIndex] view];
// Transition using a page curl.
[UIView transitionFromView:fromView
toView:toView
duration:0.5
options:(controllerIndex > tabBarController.selectedIndex ? UIViewAnimationOptionTransitionCurlUp : UIViewAnimationOptionTransitionCurlDown)
completion:^(BOOL finished) {
if (finished) {
tabBarController.selectedIndex = controllerIndex;
}
}];
Solution 2: scroll (more complex)
A more complex solution, but gives you more control of the animation. In this example we get the views to slide on and off. With this one we need to manage the views ourselves.
// Get the views.
UIView * fromView = tabBarController.selectedViewController.view;
UIView * toView = [[tabBarController.viewControllers objectAtIndex:controllerIndex] view];
// Get the size of the view area.
CGRect viewSize = fromView.frame;
BOOL scrollRight = controllerIndex > tabBarController.selectedIndex;
// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];
// Position it off screen.
toView.frame = CGRectMake((scrollRight ? 320 : -320), viewSize.origin.y, 320, viewSize.size.height);
[UIView animateWithDuration:0.3
animations: ^{
// Animate the views on and off the screen. This will appear to slide.
fromView.frame =CGRectMake((scrollRight ? -320 : 320), viewSize.origin.y, 320, viewSize.size.height);
toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);
}
completion:^(BOOL finished) {
if (finished) {
// Remove the old view from the tabbar view.
[fromView removeFromSuperview];
tabBarController.selectedIndex = controllerIndex;
}
}];
This Solution in Swift:
extension TabViewController: UITabBarControllerDelegate {
public func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let fromView: UIView = tabBarController.selectedViewController!.view
let toView : UIView = viewController.view
if fromView == toView {
return false
}
UIView.transitionFromView(fromView, toView: toView, duration: 0.3, options: UIViewAnimationOptions.TransitionCrossDissolve) { (finished:Bool) in
}
return true
}
}
When i click a button in one viewcontroller ,i want to go to other tapbarviewcontroller
Here if you have a tabBarController with say 3 View controllers.
you are at the first View controller and you click a button which will take you to the 2nd view controller. Write this code in the Action method of the Button
self.tabBarController.selectedIndex = 1; //1 is the index of the array which is self.tabBarController.viewControllers.
Tabbar controller contains its root view controllers as an array which you can get if you print
self.tabBarController.viewControllers
So accessing for accessing any view controller you have to know its index set it as the tabBarController's selected index.
Hope this helps
Related Topics
Passing Data with Segue Through Navigationcontroller
iPhone - Didselectrowatindexpath: Only Being Called After Long Press on Custom Cell
Could Not Attach to Pid:"####" Unable to Attach
Use Logical Operator as Combine Closure in Reduce
How to Set the Cornerradius of a Uistackview
Header Displaced in Tableview with Uirefreshcontrol
Swiftui in iOS14 Keyboard Avoidance Issues and Ignoressafearea Modifier Issues
How to Set Accessibility Identifier in Interface Builder? Xcode4.2
Debugging Mobile Safari in iOS 8 and iOS 9
Info.Plist File for React Native iOS App Using Expo Sdk
How to Get Selected Text from Uitextfield in Iphone
How to Loop Video with Avplayerlooper
Will Apps That Use Telprompt Be Rejected
Is -[Uitableview Reloaddata] Asynchronous or Synchronous