我该如何设定 rootViewController
UINavigationController
通过以外的方法of initWithRootViewController
?
我想用来initWithNavigationBarClass:toolbarClass:
为NavigationController交付自定义工具栏,所以我认为我不能使用initWithRootViewController
。
我该如何设定 rootViewController
UINavigationController
通过以外的方法of initWithRootViewController
?
我想用来initWithNavigationBarClass:toolbarClass:
为NavigationController交付自定义工具栏,所以我认为我不能使用initWithRootViewController
。
Answers:
您可以通过致电解决 setViewControllers
。
像这样:
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];
[navigationController setViewControllers:@[yourRootViewController] animated:NO];
迅捷版:
let navigationController = UINavigationController(navigationBarClass: MyNavigationBar.self, toolbarClass: UIToolbar.self)
navigationController.setViewControllers([yourRootViewController], animated: false)
navigationController.navigationBar.tintColor = [UIColor blackColor];
在-(void)viewDidAppear:animated:
ViewControllers的方法中为其设置样式
drawRect
自定义项,但是它是覆盖UIToolbar
a属性的默认方法UINavigationController
。
使用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
希望这对某人有帮助。
编辑:
使用Animation更改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中
在常规设置中,请在“主界面:主界面<-this”之后删除:
class AppDelegate...
var window: UIWindow?
fun application...
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: NewYourController)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let yourNewRootView = storyboard.instantiateViewControllerWithIdentifier("yourNewRootView") as? yourNewRootView
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
UIView.transitionWithView(self.window!, duration: 0.1, options: [UIViewAnimationOptions.TransitionFlipFromRight,UIViewAnimationOptions.TransitionFlipFromLeft], animations:
{
// animation
}, completion: { (finished: Bool) -> () in
self.window?.rootViewController = nil
self.window?.rootViewController = yourNewRootView
self.window?.makeKeyAndVisible()
})