In my tab based app, on clicking one of the tabs, I want to display a modal view controller with some info.
In my app delegate's didSelectViewController method, I am adding modal view. But it occupies entire screen and hides the tab bar. I don't want to hide the tab-bar, just want to display modal-view which pops up and can be dismissed.
How do I do it?
Please help.
Thanks in advance.
Modal view controllers are always presented full screen on an iPhone. If you don't want to hide the tab bar, then you need to present this view in some other way besides modal.
I was able to "present" a new view controller, over another, UNDER the tab bar, by setting modalPresentationStyle to .currentContext.
let newViewController = UIViewController()
newViewController.view.backgroundColor = UIColor.red
newViewController.modalPresentationStyle = .currentContext
newViewController.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
present(newViewController, animated: true, completion: nil)
Edit: From more testing, the above can have some buggy behavior if someone changes the tab WHILE the newViewController is presented.
To "fix," I created a "switcher" - a UIViewController that animates between the view controllers that I want to flip UNDER the tab bar:
view.addSubview(nextView)
UIView.transition(from: currentView,
to: nextView,
duration: 0.5,
options: animation,
completion: { (_) in
currentView.removeFromSuperview()
})
In this case, currentView is the view of ViewControllerOne (the currently visible one), and the nextView is view of ViewControllerTwo (the one we want to present).
For example if secondViewController is the second viewController for your second tabbar, you should do like this:
[secondViewController.view addSubview:theViewYouWantToShow];
In iOS , Modal View Controller is always have highest priority in all view controllers available. So you can not use Modal View Controller in your case.
If you just want to show popup on the screen with back ground visibility, then just use UIAlertView. You can add OK or CANCEL button as per your requirement to remove alert view.
Or
If you want to show a full view with tab bar visibility, then add the view in a that tab as a subview. You can give it a feel like a pop up using transform property of a view.
You can present it modally by setting PresentationStyle.This style presents viewController in a square and it does not occupy full screen.
self.modalPresentationStyle = UIModalPresentationFormSheet;
You can also set the transition:
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
Related
The figure shows my storyboard, with segues as directed.
In the tabBar's landing viewcontroller (i.e. I), both tabBar and navBar are visible which is desired. However, if a segue is performed (from I) to go to another viewcontroller (here, II), I want only the navBar. I am able to hide the tabBar by using
self.tabBarController?.tabBar.isHidden = true
Next, I am able to achieve the desired result by adding navigation controller at the beginning as shown below.
This configuration will add a navbar to the viewcontrollers ahead (like splash screen) so I will have to hide the navbar in those viewcontrollers.
Is there other method that does not require to hide the bar(s) and achieve the desired effect?
You should go with your first approach. Tab bar controller have default property to hide bottom bar. See below sample code.
ViewController *viewController = [[ViewController alloc] init];
viewController.hidesBottomBarWhenPushed = YES; // This property needs to be set before pushing viewController to the navigationController's stack.
[self.navigationController pushViewController:viewController animated:YES];
This will hide your tab bar while pushing to any child controllers.
EDIT
You can also set hidesBottomBarwhenpushed from storyboard for your tabbar controller so you don’t have to write down any code.
I have built an app based on a tab bar controller. While I am on one of the tab views I want to be able to swipe my finger and switch to another tab view of which the index on the tabBarController in unknown. I am therefore calling the desired viewcontroller from its nib. The view gets swapped correctly but the problem is that it appears ABOVE the tab bar itself, covering it completely and making it become unusable! How can I push the view back or the tab bar up? Thanks
- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender
{
DesiredViewController *controllerInstance =[[DesiredViewController alloc]initWithNibName:#"DesiredViewController" bundle:nil];
controllerInstance.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controllerInstance animated:YES];
}
presentModalViewController:animated always uses whole screen: On iPhone and iPod touch devices, the view of modalViewController is always presented full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.
If you really use UITabBarController, it instantiates view controllers of its tabs. So trying to instantiate view controller of one of tabs manually is wrong. You will end in a mess of view controllers instances.
Set property selectedIndex of your UITabBarController to switch tab. I think you need this. But it will be switched without animation.
If you know only pointer of controller you want to switch to, ask viewControllers property about controller's index and than switch tab by its index:
self.selectedIndex = [[self viewControllers] indexOfObject:viewController];
I have a navigation Controller as the view of a popover, so that there is a navigation bar at the top.
On the first view there is no prompt for the navigation bar, so it remains at it's usual small size.
I then push the next view controller which does need a prompt and the bar expands, except behind the view, hiding the Title and Back button.
If I comment out the code in loadView, so that self.view is never set, then you can see the backbutton and title, but you can't click on the back button, as if it was behind another view.
I never had this problem in 3.2, only now in 4.2
Here you can set the size of popover using the following code:
self.contentSizeForViewInPopover = CGSizeMake(320, 460);
You need to set the content size of popover in view using this code and you can add this code in the viewdidLoad mehtof of the controller. Let me know if you still have any question.
I have created an UITabView application. Each view selected from the bar is a seperate controller with own nib file. I switch between them succesfully.
In the first view I have two buttons (check out the screenshot). When clicking them I want to switch to another views which are the parts of the current view controller. I use:
[self presentModalViewController:anotherViewController animated:NO];
That switches the view, but hides the UITabBar. How to keep the bar on the screen after the switch?
P.S. Sorry for the blurred image. I am not allowed to share to much info.
Well I think you are misusing the modal view controller. For a problem like this I'll say you should put them in a view controller stack using UINavigationController. Instead of making each tab a UIViewController make it a UINavigationController, then you can push and pop view controllers on it, which still show the tab bar.
See http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html
use: tabBarController.selectedViewController = newViewController
edit: UINavigationController is not needed here.
I have made an application in which I have four tab bars. I have used the below code to go to the second view in a button's click method. It goes to the second view but the second view is not showing with tab bars. How can I fix this?
- (IBAction)Existinguser:(id)sender
{
ExistingUserViewController *existingUserViewController =
[[ExistingUserViewController alloc]
initWithNibName:#"ExistingUserViewController" bundle:nil];
[self presentModalViewController:existingUserViewController animated:YES];
[existingUserViewController release];
}
Presenting a view controller modally will show that view controller above all ther other UI elements (this includes the navigation bar and tab bar, but not the status bar).
If you want to show your "second view" as the view on the second tab, then you need to set the view controller you want to use as the second tab's view controller. That way when you press the second tab, the second view controller will be shown, and the second tab will be selected and visible.
I might be mistaken, but do you have a single tab bar with four tabs and you are looking for a way to change to the second tab? You can do this with the following code:
self.tabBarController.selectedIndex = 1;
btw the code you attached to your question is displaying a new model view controller above the current view controller. This is the reason no tabbar is shown.
If you are trying to create a navigation based app using tabbar, in IB, change the tab's viewcontroller to UINavigationController. You can then use the UINavController methods to navigate between views. In case you don't want to show the navbar, just set that property in IB.
Here's a good example of how you can use tab bar with navbar