I'm trying to use the following code to display a tabbarcontroller
UITabBarController *tc = [[self storyboard] instantiateViewControllerWithIdentifier:#"tabbarcontroller"];
[self.navigationController pushViewController:tc animated:YES];
It does load the view, and I can tell it which of the tabs I want it to default to. The problem is the tabs don't show. From what I've read I gather it has something to do with putting the tab controller inside of the navigation controller, but I couldn't find any solutions.
If you use Storyboard, use pushViewController method is a bad choice (also if it work). You have to insert a "segue".
Go in the storyboard and while press ctrl button, click on the main controller (which must open the tabViewController) and then release the click on the tabBarController.
Now you have the segue. Click on the circle which appears and choose an identifier for this segue, for example: MainToTab .
Now in your method, you have just to call:
[self performSegueWithIdentifier:#"MainToTab" sender:self];
Moreover, if you want manage the property on the destination controller (by segue), you can implement this method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"MainToTab"]) {
UITabViewController *tb = (UITabViewController *)segue.destinationViewController;
//set the properties...
}
}
This method is called automatically when you launch the previous method.
What you want inside your UITabBarController are UIViewControllers, possibly inside a UINavigationController.
You shouldn't as well push a UITabBarController. You can see Apple's explanation here:
An app that uses a tab bar controller can also use navigation
controllers in one or more tabs. When combining these two types of
view controller in the same user interface, the tab bar controller
always acts as the wrapper for the navigation controllers.
The most common way to use a tab bar controller is to embed its view
in your app’s main window. (...)
Still you can present it modally:
It is possible (although uncommon) to present a tab bar controller
modally in your app. Tab bar interfaces are normally installed in your
app’s main window and updated only as needed. However, you could
present a tab bar controller modally if the design of your interface
seems to warrant it. For example, to toggle from your app’s primary
operational mode to a completely different mode that uses a tab bar
interface, you could present the secondary tab bar controller modally
using a crossfade transition.
Related
I have implemented a tab bar, but as I go through the app I am not seeing the tab bar. It disappears after I go to a certain page. This is how I implemented it. I have a tab bar connected to a vc which is embedded inside a navigation controller. So the hierarchy looks like this.
----UITabBarController
-------UINavigationController
-----------ViewController 1 with button to view controller 2 (I can see the tab bar)
----------------View Controller 2 (I can't see the tab bar)
It sounds like the segue that you get from view controller 1 to view controller 2 is a "present" segue, rather than a "push" segue. (If I recall correctly, Apple removed "push" segues from Storyboards recently.) Sadly, "present"ed view controllers appear in front of the navigation controller.
In order to do a "push" segue, you have to do it in code, e.g.:
- (IBAction)buttonTapped: (id)sender
{
ViewController2 *viewController = ...
[self.navigationController pushViewController:viewController];
}
i think you missed to initial tab bar as initial view controller
i have a tab bar application
i am using storyboards and also navigation bar in my app
in my app
FirstViewController (in tab bar) with a button go to the SingleViewController (xib file and not on tab bar)
in SingleViewController there is a button too. and i want to go to one of the controllers which is on tab bar
in singleviewController's button method i have this code:
AylikGirisViewController *controller = [[AylikGirisViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
it goes to the controller but i see a black screen and empty screen also moving to the AylikGirisViewController gets hard (i mean very slowly)
so i think my way is wrong
any ideas for the right way?
if you are using storyboard, you can simply drag another view controller onto the storyboard and give name as SingleViewController, you can easily push the SingleViewController onto the FirstViewController if you embed the FirstViewController in UINavigationController. Use storyboard segue to push the controller. Then you can call UITabBarController Delegate method to show the different viewController.
I'm using the following code to perform a segue to another view controller:
[self performSegueWithIdentifier:#"BackSegue" sender:self];
This works fine when the destination of "BackSegue" named segue is another view controller (one of the tabs, actually) but I need to display the tabs at the bottom so transitioning directly to this view controller won't work as there will be no tabs. Is it allowed/possible to segue to a tabbar controller? Is anything wrong with this specific code or would it be something else I'm doing?
Edit 1
The TabBar controller has no .m/.h files and is never declared programmatically, but I'm pretty sure the segue is set up correctly in the storyboard to the best of my knowledge (the same way it was set up earlier directly to the other viewcontroller).
You can segue directly to a UITabBarController. Just change the segue in your storyboard. When the segue occurs, it should load the tab bar controller, and consequently the tab bar at the bottom of the screen and the first view controller's view associated with the tab bar controller.
In order to segue to the specific tab in the tab bar controller:
You need to add the selectedIndex=1
Add these lines of code for segue:
UITabBarController *loadTabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TasksAppsTabs"];
loadTabBar.selectedIndex=1;
[self presentViewController:loadTabBar animated:YES completion:nil];
I have tab bar and in view "A" and in "A" I have navigation controller. SO inside my navigation controller in "A" i called
[self presentModalViewController:modalView animated:YES]
But modalView shows under tab bar. How to show it above tab bar?
Try to present modal view from UITabBarController:
[self.tabBarController presentModalViewController:modalView animated:YES];
My case, the modal has a transparent background, I set modalPresentationStyle is .overFullScreen and it show at above tabbar with clear background.
In my case the presented view controller had UIModalPresentationStyle.CurrentContext at .modalPresentationStyle, which made the tab bar overlap
Jus switch back to a default value to fix the issue
The cause of this is that the ancestor viewController is not correctly set.
for instance imagine:
UIViewController * myController = ... // a view controller without a proper ancestor
now:
myController.tabBarController == nil
Therefore:
[myController presentModalViewController:otherController];
Will fail (by showing up under the tab bar). The fix is to add myController to its ancestor via
[parentController addChildViewController:myController];
Now, parentController must be added to another controller in the same way and so forth until the root one is your tabController. This is only available in iOS 5+. If build for iOS 4 or earlier, you will have to work around this by making sure all of your controllers are directly added to either a UINav or UITab controller. If this is not possible, you will have to access the UITabBarController via a global variable.
I have a class that is of type UITableViewController.
This class has a member of type UINavigationBar that I use for adding in an edit button for the table view.
Using this method calling is invalid.
[self.navigationController pushViewController:controller];
How can I push a detail view onto the view after selecting a table row without wrapping my UITableViewController in a UINavigationController?
The closest alternative if you don't want to use navigation controller are modal view controllers.
[self presentViewController:controller animated:YES completion:nil];
This will slide the controller into your screen from bottom, or if you change controller's modalTransitionStyle it can flip over or fade in.
To dismiss the controller just call:
[self dismissViewControllerAnimated:YES completion:nil];
I would wrap the UITableView inside a UINavigationController and just hide the UINavigationBar.
[self.navigationController setNavigationBarHidden:YES animated:NO];
And then create a back button that pops the ViewController off the stack.
[self.navigationController popViewControllerAnimated:YES]
What could also be done is to use a Navigation Controller as usual and then hide it.
To hide the Navigation Controller using storyboards: select it and uncheck "Show Navigation Bar" in the attribute inspector. Others might suggest to hide the navigation bar in each controller, but the problem with that is that it will appear for a millisecond and then disappear.
You can't push a view controller onto a navigation controller if there is no navigation controller. If you are wanting to be pushing controllers and have it display the topmost controller and everything, just use a UINavigationController and be done with it.
You can push arbitrary UINavigationItems onto your UINavigationBar, and your bar's delegate will be notified when the user uses the back button so you can take appropriate action. See the documentation for more information.
It's true that without a UINavigationController you can not push view controllers. You rather present view controllers modally via UIViewController.present(_ viewControllerToPresent:, animated:, completion:)
But it's possible to create a custom segue to display the view controller as if it were a push (or any other animation you want), although it seems that using a UINavigationController just makes things easier.
Here are some related links to the documentation:
UINavigationController Class Reference
Customizing the Transition Animations
Presenting a Modal View Controller