ios presentViewController 和显示导航栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9724194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:20:06  来源:igfitidea点击:

presentViewController and displaying navigation bar

iosiphoneuiviewcontrollermodalviewcontrollerpresentmodalviewcontroller

提问by Jonas Gardner

I have a view controller hierarchy and the top-most controller is displayed as a modal and would like to know how to display the navigation bar when using

我有一个视图控制器层次结构,最顶层的控制器显示为模态,想知道如何在使用时显示导航栏

'UIViewController:presentViewController:viewControllerToPresent:animated:completion'

The docs for 'presentViewController:animated:completion:' note:

'presentViewController:animated:completion:' 的文档注意:

'On iPhone and iPod touch, the presented view is always full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.'

'在 iPhone 和 iPod touch 上,呈现的视图始终是全屏的。在 iPad 上,演示文稿取决于 modalPresentationStyle 属性中的值。

For 'modalPresentationStyle', the docs say:

对于“modalPresentationStyle”,文档说:

The presentation style determines how a modally presented view controller is displayed onscreen. On iPhone and iPod touch, modal view controllers are always presented full-screen, but on iPad there are several different presentation options.

呈现风格决定了模态呈现的视图控制器如何在屏幕上显示。在 iPhone 和 iPod touch 上,模态视图控制器总是全屏显示,但在 iPad 上有几种不同的显示选项。

Is there way to ensure that the navigation bar is visible below the status bar once the view control displays itself? Should I interpret the doc as, you don't get any options of iPhone/iPod and only on iPad?

一旦视图控件显示自身,有没有办法确保导航栏在状态栏下方可见?我应该将文档解释为,您没有 iPhone/iPod 的任何选项,只能在 iPad 上使用吗?

Previously, I was using 'UIViewController:presentModalViewController:animated'which worked fine, but since iOS 5.0, the API has been deprecated so I'm switching over to the new one.

以前,我使用'UIViewController:presentModalViewController:animated'它运行良好,但自 iOS 5.0 以来,该 API 已被弃用,因此我正在切换到新的 API。

Visually, what I'm looking to do is have the new controller slide in from the bottom of the screen, just like the old API used to do.

从视觉上看,我想要做的是让新控制器从屏幕底部滑入,就像以前的旧 API 一样。

[updating with code]:

[更新代码]:

// My root level view:
UIViewController *vc = [[RootViewController alloc] 
                            initWithNibName:nil 
                            bundle:[NSBundle mainBundle]];
navController = [[UINavigationController alloc] initWithRootViewController:vc];        
....

// Within the RootViewController, Second view controller is created and added 
// to the hierarchy. It is this view controller that is responsible for 
// displaying the DetailView:
SecondTierViewController *t2controller = [[SecondTierViewController alloc] 
                                           initWithNibName:nil
                                           bundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:t2controller animated:YES];

// Created by SecondTierViewController 
DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil                                                                                 
                                        bundle:[NSBundle mainBundle]];  

controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.modalPresentationStyle = UIModalPresentationCurrentContext;

[self.navigationController presentViewController:controller 
                                        animated:YES 
                                        completion:nil];

回答by Manish Ahuja

It is true that if you present a view controller modally on the iPhone, it will always be presented full screen no matter how you present it on the top view controller of a navigation controller or any other way around. But you can always show the navigation bar with the following workaround way:

确实,如果您在 iPhone 上以模态方式呈现视图控制器,无论您如何将其呈现在导航控制器的顶部视图控制器上或以任何其他方式呈现,它都将始终以全屏方式呈现。但是您始终可以使用以下解决方法显示导航栏:

Rather than presenting that view controller modally present a navigation controller modally with its root view controller set as the view controller you want:

而不是以模态方式呈现该视图控制器,而是以模态方式呈现导航控制器,其根视图控制器设置为您想要的视图控制器:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController = 
    [[UINavigationController alloc] initWithRootViewController:myViewController];

//now present this navigation controller modally 
[self presentViewController:navigationController
                   animated:YES
                   completion:^{

                        }];

You should see a navigation bar when your view is presented modally.

当您的视图以模态呈现时,您应该会看到一个导航栏。

回答by Tal Zion

Swift 5.*

Swift 5.*

Navigation:

导航:

guard let myVC = self.storyboard?.instantiateViewController(withIdentifier: "MyViewController") else { return }
let navController = UINavigationController(rootViewController: myVC)

self.navigationController?.present(navController, animated: true, completion: nil)

Going Back:

返回:

self.dismiss(animated: true, completion: nil)

Swift 2.0

斯威夫特 2.0

Navigation:

导航:

let myVC = self.storyboard?.instantiateViewControllerWithIdentifier("MyViewController");
let navController = UINavigationController(rootViewController: myVC!)

self.navigationController?.presentViewController(navController, animated: true, completion: nil)

Going Back:

返回:

self.dismissViewControllerAnimated(true, completion: nil)

回答by marrop

Can you use:

你能用:

[self.navigationController pushViewController:controller animated:YES];

Going back (I think):

回去(我认为):

[self.navigationController popToRootViewControllerAnimated:YES];

回答by Mohammad Parvez

I had the same problem on ios7. I called it in selector and it worked on both ios7 and ios8.

我在ios7上遇到了同样的问题。我在选择器中调用它,它适用于 ios7 和 ios8。

[self performSelector: @selector(showMainView) withObject: nil afterDelay: 0.0];

- (void) showMainView {
    HomeViewController * homeview = [
        [HomeViewController alloc] initWithNibName: @
        "HomeViewController"
        bundle: nil];
    UINavigationController * navcont = [
        [UINavigationController alloc] initWithRootViewController: homeview];
    navcont.navigationBar.tintColor = [UIColor whiteColor];
    navcont.navigationBar.barTintColor = App_Theme_Color;
    [navcont.navigationBar
    setTitleTextAttributes: @ {
        NSForegroundColorAttributeName: [UIColor whiteColor]
    }];
    navcont.modalPresentationStyle = UIModalPresentationFullScreen;
    navcont.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController presentViewController: navcont animated: YES completion: ^ {

    }];
}

回答by Anil Prasad

I use this code. It's working fine in iOS 8.

我用这个代码。它在 iOS 8 中运行良好。

MyProfileEditViewController *myprofileEdit=[self.storyboard instantiateViewControllerWithIdentifier:@"myprofileeditSid"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myprofileEdit];
[self presentViewController:navigationController animated:YES completion:^{}];

回答by Ignat

All a [self.navigationController pushViewController:controller animated:YES];does is animate a transition, and add it to the navigation controller stack, and some other cool navigation bar animation stuffs. If you don't care about the bar animation, then this code shouldwork. The bar does appear on the new controller, and you get an interactive pop gesture!

a[self.navigationController pushViewController:controller animated:YES];所做的只是为过渡设置动画,并将其添加到导航控制器堆栈中,以及其他一些很酷的导航栏动画内容。如果你不关心条形动画,那么这段代码应该可以工作。该栏确实出现在新控制器上,您将获得一个交互式弹出手势!

//Make Controller
DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil                                                                                 
                                    bundle:[NSBundle mainBundle]];  
//Customize presentation
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.modalPresentationStyle = UIModalPresentationCurrentContext;

//Present controller
[self presentViewController:controller 
                   animated:YES 
                 completion:nil];
//Add to navigation Controller
[self navigationController].viewControllers = [[self navigationController].viewControllers arrayByAddingObject:controller];
//You can't just [[self navigationController].viewControllers addObject:controller] because viewControllers are for some reason not a mutable array.

Edit: Sorry, presentViewController will fill the full screen. You will need to make a custom transition, with CGAffineTransform.translation or something, animate the controller with the transition, then add it to the navigationController's viewControllers.

编辑:抱歉,presentViewController 将填满整个屏幕。您需要使用 CGAffineTransform.translation 或其他东西进行自定义转换,使用转换为控制器设置动画,然后将其添加到 navigationController 的 viewControllers 中。

回答by ioopl

Swift version : This presents a ViewController which is embedded in a Navigation Controller.

Swift 版本:这提供了一个嵌入在导航控制器中的 ViewController。

    override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    //  Identify the bundle by means of a class in that bundle.
    let storyboard = UIStoryboard(name: "Storyboard", bundle: NSBundle(forClass: SettingsViewController.self))

    // Instance of ViewController that is in the storyboard.
    let settingViewController = storyboard.instantiateViewControllerWithIdentifier("SettingsVC")

    let navController = UINavigationController(rootViewController: settingViewController)

    presentViewController(navController, animated: true, completion: nil)

}

回答by BennyTheNerd

Swift 3

斯威夫特 3

        let vc0 : ViewController1 = ViewController1()
        let vc2: NavigationController1 = NavigationController1(rootViewController: vc0)
        self.present(vc2, animated: true, completion: nil)

回答by rakeshNS

If you didn't set the modalPresentationStyle property (like to UIModalPresentationFormSheet), the navigation bar will be displayed always. To ensure, always do

如果您没有设置 modalPresentationStyle 属性(如 UIModalPresentationFormSheet),导航栏将始终显示。为了确保,始终做

[[self.navigationController topViewController] presentViewController:vieController 
                                                            animated:YES 
                                                          completion:nil];

This will show the navigation bar always.

这将始终显示导航栏。

回答by Yatin Sarbalia

One solution

一种解决方案

DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil                                                                                 
                                        bundle:[NSBundle mainBundle]];  

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
navController.modalPresentationStyle = UIModalPresentationCurrentContext;



[self.navigationController presentViewController:navController 
                                        animated:YES 
                                        completion:nil];