Установите rootViewController из UINavigationController с помощью метода, отличного от initWithRootViewController
Как установить rootViewController
из UINavigationController
с помощью метода, отличного от initWithRootViewController
?
Я хочу использовать initWithNavigationBarClass:toolbarClass:
для доставки пользовательской панели инструментов для моего навигационного контроллера, поэтому я не думаю, что могу использовать initWithRootViewController
.
Ответы
Ответ 1
Вы можете решить это, вызвав setViewControllers
.
Вот так:
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];
[navigationController setViewControllers:@[yourRootViewController] animated:NO];
Ответ 2
Обмен знаниями с помощью Swift:
Изменение контроллера корневого представления из класса, отличного от приложения 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)
Мы можем написать метод обобщения, слишком похожий на this.
Ответ 3
этот работает для меня, надеюсь, что это поможет вам,
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
Ответ 4
В swift 3.0 xcode8.1
в общих настройках delete в главном интерфейсе: Main < -this
после основного интерфейса:
class AppDelegate...
var window: UIWindow?
fun application...
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: NewYourController)
Ответ 5
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()
})