ChatGPT解决这个技术问题 Extra ChatGPT

presentViewController 和显示导航栏

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

'UIViewController:presentViewController:viewControllerToPresent:animated:completion'

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

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

对于“modalPresentationStyle”,文档说:

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

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

以前,我使用的 'UIViewController:presentModalViewController:animated' 运行良好,但从 iOS 5.0 开始,该 API 已被弃用,因此我将切换到新的。

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

[使用代码更新]:

// 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];

m
mfaani

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

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

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:^{

                        }];

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


这几乎是我开始的。但我不使用 'presentModalViewController' 的原因是因为它被标记为已弃用的 API。
这是在 UIViewController 类中编写的内容: // 将另一个视图控制器显示为模态子级。如果动画,则使用垂直工作表过渡。此方法已被 presentViewController:animated:completion: // 它将被弃用,相应地计划。 - (void)presentModalViewController:(UIViewController *)modalViewController 动画:(BOOL)animated;所以只需调用新方法并传递 nil 即可完成,你应该会很好。
很好的答案。更新为使用 (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
当我尝试展示导航控制器时,它会崩溃 ('NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported')。这怎么行?
在我的情况下,它显示栏,但其他内容被错误地放置在呈现动画上。只有在这个动画之后它才会跳到正确的位置。
T
Tal Zion

斯威夫特 5.*

导航:

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

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

回去:

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

斯威夫特 2.0

导航:

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

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

回去:

self.dismissViewControllerAnimated(true, completion: nil)

但是当我尝试你的代码时如何设置,然后只设置导航栏,但我不能更改它的属性,如条形颜色、标题等。
与此问题无关,但您可以在此处找到答案stackoverflow.com/questions/26008536/…
m
marrop

你可以使用:

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

回去(我认为):

[self.navigationController popToRootViewControllerAnimated:YES];

谢谢,如果您的故事板中已经有导航控制器设计,最好的方法是这样做。你帮了我很多
回去是 [self.navigationController popViewControllerAnimated:YES]; popToRoot - 一直回到第一个视图控制器
m
methode

我在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: ^ {

    }];
}

I
Ignat

[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.

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


B
BennyTheNerd

斯威夫特 3

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

如何将返回按钮添加到呈现的 UIViewController
T
TheCodingArt

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)

}

s
shim

我使用此代码。它在 iOS 8 中运行良好。

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

Y
Yatin Sarbalia

一种解决方案

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];

A
Alex Cio

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

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

这将始终显示导航栏。


嗯..即使对“topViewController”的固定引用,我仍然看到相同的行为。我认为我不会以任何特殊方式将其他视图控制器添加到导航堆栈中。
E
Ego Slayer

如果你在 Swift 2.x 中使用 NavigationController

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let targetViewController = storyboard.instantiateViewControllerWithIdentifier("targetViewControllerID") as? TargetViewController
self.navigationController?.pushViewController(targetViewController!, animated: true)

t
tsinghan

尝试这个

     let transition: CATransition = CATransition()
    let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.duration = 1
    transition.timingFunction = timeFunc
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromRight
    self.view.window!.layer.addAnimation(transition, forKey: kCATransition)
    self.presentViewController(vc, animated:true, completion:nil)

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅