简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM