Ios 使用initWithRootViewController以外的方法设置UINavigationController的rootViewController

Ios 使用initWithRootViewController以外的方法设置UINavigationController的rootViewController,ios,uinavigationcontroller,uitoolbar,iphone-5,Ios,Uinavigationcontroller,Uitoolbar,Iphone 5,如何通过initWithRootViewController以外的方法设置UINavigationController的rootViewController 我想使用initWithNavigationBarClass:toolbarClass:为我的NavigationController提供一个自定义工具栏,因此我认为我不能使用initWithRootViewController您可以通过调用setViewController来解决这个问题 像这样: UINavigationControll

如何通过
initWithRootViewController
以外的方法设置
UINavigationController
rootViewController


我想使用
initWithNavigationBarClass:toolbarClass:
为我的NavigationController提供一个自定义工具栏,因此我认为我不能使用
initWithRootViewController

您可以通过调用
setViewController
来解决这个问题

像这样:

UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];

[navigationController setViewControllers:@[yourRootViewController] animated:NO];
Swift版本:

let navigationController = UINavigationController(navigationBarClass: MyNavigationBar.self, toolbarClass: UIToolbar.self)

navigationController.setViewControllers([yourRootViewController], animated: false)

使用Swift进行知识共享:

正在从类(而不是app delegate.swift)更改根视图控制器

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav
希望这将有助于某人

编辑:

使用动画更改rootviewcontroller可以通过以下方式实现:

 UIView.transitionWithView(self.window!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
    self.window?.rootViewController = anyViewController
}, completion: nil)

我们可以写太类似于

的泛化方法,这个方法对我很有效,希望对你有帮助

let rootVC:LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
let nvc:UINavigationController = self.storyboard?.instantiateViewControllerWithIdentifier("RootNavigationController") as! UINavigationController
nvc.viewControllers = [rootVC]
UIApplication.sharedApplication().keyWindow?.rootViewController = nvc
在swift 3.0 xcode8.1中


在常规设置中,在主界面中删除:我刚刚发现的主要方法是:[navigationController SetViewController:[NSArray arrayWithObject:(指向应为根视图控制器的视图控制器的指针)];这是最好的方式吗?我认为您应该只在DidFinishLaunchingDelegate方法的AppDelegate文件中调用此函数,因为在此之后使用UINavigationController的视图控制器数组似乎有风险,谢谢您的确认。这可能需要在一个单独的问题中进行,但我假设initWithNav。。这意味着我的所有工具栏都将具有我在自定义子类中设置的相同外观。那么,如何为我的应用程序的不同部分设置不同样式的工具栏?我是否从该自定义子类返回不同的工具栏?或者有更好的方法吗?为此,您可以使用
navigationController.navigationBar.tintColor=[UIColor blackColor]
或您希望在ViewControllers的
-(void)ViewDidDisplay:animated:
方法中对其进行样式设置的方式。因此,这将覆盖以下事实:在我的UIToolBar子类中,我覆盖drawRect并设置自定义背景?我就是这样实现的:UIImage*backgroundImage=[UIImage-imagename:@“UIToolBar_Background.png];[backgroundImage drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];我不确定它是否覆盖了
drawRect
定制,但它是覆盖
UINavigationController
UIToolbar
属性的默认方法。我在遇到的问题上发布了关于这个后续问题的任何想法。嗨@Arvind,你能告诉我这个密码放在哪里最好吗?它是否应该位于AppDelegate.swift的顶部,以便变量全局可用?或者它需要在类的某个地方,因为它正在实例化视图?谢谢,您可以在func应用程序中使用(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?)->Bool。而不是在任何视图控制器上。当您要求注销时,您可能需要这样做。@Arvind如何处理动画?不显示动画,根视图立即显示。@Engnyl:我已经更新了答案。感谢您的查询,这使它更加更新。在self.navigationcontroller上获得零。您救了我一天!一个人拿零分。navigationController@WasimAhmed这意味着您没有将导航控制器作为根视图控制器这并没有回答问题。您正在使用RootViewController调用init,这正是问题所在,即如何避免。
let rootVC:LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
let nvc:UINavigationController = self.storyboard?.instantiateViewControllerWithIdentifier("RootNavigationController") as! UINavigationController
nvc.viewControllers = [rootVC]
UIApplication.sharedApplication().keyWindow?.rootViewController = nvc
class AppDelegate...

 var window: UIWindow?

    fun application...

      window = UIWindow(frame: UIScreen.main.bounds)
      window?.makeKeyAndVisible()
      window?.rootViewController = UINavigationController(rootViewController: NewYourController)