How to present a UIViewController on top of my current UIViewController? - ios

I'm doing everything programmatically. Not using storyboards.
I have a navigation bar at the bottom of my screen (like a tab bar, but custom made), with 5 options. I'd like to present a different view controller when each of those navigation options is clicked. However, I don't want the presented view controllers to cover my navigation bar at the bottom.
Ex: I have a settings button that can be pressed. I also have a SettingsViewController file. When a user clicks on the 'settings' button, I do
let settingsViewController = SettingsViewController()
settingsViewController.modalPresentationStyle = .OverCurrentContext
self.presentViewController(SettingsViewController(), animated: false, completion: nil)
but this covers my entire screen with a new view. When I change the color of this new view to UIColor.clearColor(), I can still see my navigation bar at the bottom but I cannot interact with it (the new view is on top of it).
I have tried to change the frame of my SettingsViewController view so that it does not cover the entire screen by putting
self.view.frame = CGRectMake(0,0,width,height-100)
in viewDidLoad() in my SettingsViewController. But the view still covers the entire screen!
I want my SettingsViewController to display only from the [top to 100px from the bottom] area of the screen. I want my navigation bar to remain in the bottom 100px of the screen. How can I do this?

Figured it out :)
I was trying to present my new SettingsViewController when all I needed to do was add the view from my SettingsViewController as a subview.
The solution is to do
self.view.addSubview(settingsViewController.view)

Related

What is best approach to creating a persistant UIView nav bar in a UITabBarController?

I've added a custom UIView to my base UITabBarController. I start by hiding the default tabBar. The viewdidload looks like this in UITabBarController:
override func viewDidLoad() {
super.viewDidLoad()
//hide default tab bar
self.tabBar.isHidden = true
tabBarArea.frame.size.width = self.view.frame.width
tabBarArea.frame.origin.y = self.view.frame.height - tabBarArea.frame.height
self.view.addSubview(tabBarArea)
}
That works well. The tabBarArea is defined in the storyboard as a custom view for the UITabBarController. The custom view sits between the First Responder and the Exit icons in the top bar.
Now, the problem is that the UITabBarController will disappear as soon as we load a child view controller and this custom UIView area will vanish with it.
Is there a way to make this root custom view always present even when child view controllers are loaded in?
Thanks for input. I like the idea of this custom UIView area but this approach needs refinement. I also don't need it to be a traditional UITabBarController with tab bar items, etc. I'd like to break out of that mold and just have custom UIButtons are whatever in this view area.

How to overlap navigation bar by adding view in swift?

I want to make a custom side bar by adding a new view to the view controller, the side bar will be in the yellow color background. I want my side bar also to overlap the navigation bar/item (green background color) in my view controller. but the navigation bar/item seems can't be overlapped by my side bar view, it seems only overlap the main view.
I tried to find the answer in stackoverflow, I find this Overlap navigation bar on ios 6 with other view, but the answer is on the Objective-C, I can't read Objective-C :(
What should I do to overlap navigation bar/item ? here is the screenshot of my view controller
I embed the navigation controller like this
There are plenty of implementations of slide-over or drawer containers.
What you need to do to get above the navigation bar is CONTAIN the navigation controller inside another view controller.
The stack would look like this.
MasterViewController
UINavigationController
RootViewController
Menu
See this one here:
Swift version of MMDrawerController
You can do this by changing your UIViewController hierarchy. For this you'll need three view controllers. First will contain everything, let's call it MasterViewController; second—your main content with navigation bar; and third—drawer.
In MasterViewController instantiate child view controllers and add them to your view controller hierarchy in viewDidLoad().
final class MasterViewController: UIViewController {
override func viewDidLoad() {
let drawerViewController = DrawerViewController()
let mainViewController = MainContentViewController()
let navigationController = UINavigationController(rootViewController: mainViewController)
addChildViewController(drawerViewController)
addChildViewController(navigationController)
view.addSubview(navigationController.view)
view.addSubview(drawerViewController.view)
}
}
Now you have navigationController.view that you can place or animate anywhere within view.

Swift: present view controller in same context as other view controllers?

Ok, I have an issue that I cant understand trying to present a view controller (the same instance every time, just like other tab item VCs) from an overall tab bar controller VC. My tab bar controller VC has 3 view controllers that it is connected to via storyboard, so 3 tab bar items appear on the tab bar. When the selectedIndex is changed, these view controllers just appear right there below the subviews of the Tab Bar Controller VC.
These subviews that should always be on top are the nav bar at the top and tab bar at bottom:
And this is great for those 3 view controllers. Problem is I need to access 1 instance of ANOTHER view controller that is NOT shown in the tab bar buttons via a button in the nav bar here.
My problem is no matter how I present it, this VC always pops OVER the tab bar controller VC, covering the tab bar and nav bar.
here I make sure only 1 instance is made:
if podcastVC == nil {
//print("IT IS NIL")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
podcastVC = storyboard.instantiateViewController(withIdentifier: "podcast") as! PodcastViewController
//*NOTE: have to set other vars too, this is temp
podcastVC.urlStr = currentTrackUrl!
podcastVC.originalUrl = currentTrackUrl!
AudioPlayerManager.shared.play(urlString: podcastVC.urlStr)
}
self.show(podcastVC, sender: self)
podcastVC.modalPresentationStyle = .currentContext
podcastVC.definesPresentationContext = false
[1]: https://i.stack.imgur.com/1d6MZ.png
as shown by Swift: How to return to the same instance of my UIViewController
How can I make that VC present in the same context as the tab bar items? I have tried setting the layer of the nav bar to a z position much higher (like 10) but nothing works. What is wrong?
Modal view :
Can works for all view controllers
Is over all other view and need to be pop programatically (adding a button back manually for example)
Push View :
Only works in navigation controllers
Add automatically a back button in the navigationController
you should push VC and it will keep tabbar and nav
you can change modal present style

Set a view above navigation controller and below popupView

I have a collection View with navigation bar on StoryBoard. There is a popupView that appears when custom cell is selected.
On main StoryBoard I set a View dimView to make dark background color while pop up view is showing.
However when I test on device, dimView does not go above Navigation Bar or TabBar.
I tried following code let window = UIApplication.shared.keyWindow! window.addSubview(dimView). But it makes dimView above pop up View.
I would like to set a view above navigation controller and below popupView.
Any idea how to solve this?
Have You try adding dimview on window but below popUp like this
let window = UIApplication.shared.keyWindow!
window.insertSubview(dimView, belowSubview: popupView)

alternating between toolbar / tab bar

my app is structured as follow: UITabBarController > UINavigationController > ViewControllerOne > ViewControllerTwo.
the UINavigationBar has at the bottom the tab bar, now when the user navigates into the second view controller, i want to be able to hide the tab bar and replace is with a tool bar. i tried this code:
[self.navigationController.tabBarController.tabBar setHidden:YES];
[self.navigationController.toolbar setHidden:NO];
when i run the app the tab bar is hidden but the toolbar doesn't appear. plus, since the last VC is a table view controller, when i scroll through the cells there is a white gap between the table and the bottom of the view. how can i fix that?
That won't work because when you hide the tab bar like that the subviews won't be adjusted properly (that's why you get the white space). You'll have to use
self.hidesBottomBarWhenPushed = YES;
In your init method or awakeFromNib... and then
[self.navigationController setToolbarHidden:NO animated:YES];
In the viewDidLoad for example.
That way the tab bar controller's view is going to layout correctly it's subviews when you hide the tab bar. Just remember to call self.hidesBottomBarWhenPushed = NO; in your first view controller otherwise the tab bar is still going to be hidden when the second view controller is popped from the navigation stack.
Try to assigning toolbar with appropriate frame and adding it to self.tabBarController.view

Resources