简体   繁体   English

从UIViewController推送UINavigationController

[英]Push UINavigationController from UIViewController

I would push a UINavigationController in the same way Favorite/Bookmarks Folders are presented in iOS Safari: 我将以与iOS Safari中的“收藏夹/书签”文件夹相同的方式推送UINavigationController:

Safari推动画

I already tried using a single UINavigationController, setting navigationController?.setNavigationBarHidden(true, animated: false) in viewWillAppear for the first view, but I can't get the desidered behavior: I would have the navigationbar to slide in when child viewControllers are pushed and to slide out when returning to the rootViewController. 我已经尝试过使用单个UINavigationController,在第一个视图的viewWillAppear设置navigationController?.setNavigationBarHidden(true, animated: false) ,但我无法获得期望的行为:当按下子viewControllers时,导航栏会滑入并在返回到rootViewController时滑出。

Any suggestion? 有什么建议吗? I'm using Swift 4.0.3, developing target iOS 11, thanks in advance. 我使用的是Swift 4.0.3,因此要开发目标iOS 11。

Edit 编辑

I tried to achieve the wanted behavior by nesting two UINavigationControllers: 我试图通过嵌套两个UINavigationControllers来实现所需的行为:

mainNavController: UINavigationController           // with hidden NavigationBar
  - firstFavoritePage: UIViewController             // as rootViewController
  - wrapper: UIViewController                       // a containerViewController
    - nestedNavController: UINavigationController
      - blankPageWithTitleAll: UIViewController     // as rootViewController
      - folderPage: UIViewController

I added a wrapper container ViewController since two UINavigationControllers are merged while directly nested (to have UISplitViewController working as expected) and I added a blank page in the inner NavigationController with the title "All" to get the "< All" back button to animate correctly on pushes (a custom back button fades out, a real back button not). 我添加了一个包装容器ViewController,因为两个UINavigationControllers在直接嵌套时合并了(以使UISplitViewController正常工作),并且在内部NavigationController中添加了一个空白页,标题为“ All”,以使“ <All”后退按钮正确地进行动画处理按下时(自定义的后退按钮会逐渐消失,而真正的后退按钮则不会)。

In Swift: 在Swift中:

let wrapper = UIViewController()
let blankPageWithTitleAll = UIViewController()
let folderPage = UIViewController()
let nestedNavController = UINavigationController(rootViewController: blankPageWithTitleAll
wrapper.addChildViewController(nestedNavController)
wrapper.view.addSubview(nestedNavController.view)
nestedNavController.view.anchorToSuperview(wrapper.view)
nestedNavController.didMove(toParentViewController: wrapper)
nestedNavController.pushViewController(folderPage, animated: false)
blankPageWithTitleAll.navigationItem.title = "All"
firstFavoritePage.show(wrapper, sender: self)

Now the presenting animation works as expected, but to dismiss everything, if the active ViewController is the second or first controller of the nestedNavController navigation stack, I should: 现在,呈现的动画可以按预期工作,但是要消除所有内容,如果活动的ViewController是nestedNavController导航堆栈的第二个或第一个控制器,我应该:

  • prevent the nestedNavController interactive pop transition; 防止nestedNavController交互式弹出过渡;
  • prevent the nestedNavController back button pop transition; 防止nestedNavController后退按钮弹出过渡;
  • add a custom action when the "< All" back button is pressed to pop the mainNavController instead of the nestedNavController; 当按下“ <全部”后退按钮以弹出mainNavController而不是nestedNavController时,添加一个自定义操作;
  • [probably the most difficult thing to do] mirror the pop UIPercentDrivenInteractiveTransition (the edge swipe to go back interactive animation) of the nestedNavController to the mainNavController. [可能是最困难的事情]将nestedNavController的弹出UIPercentDrivenInteractiveTransition(边缘滑动以返回交互式动画)镜像到mainNavController。

I don't even know if I can get or set percentage values for that animated transition (I don't think it could be possible.. but I'll try to figure out if it is). 我什至不知道是否可以获取或设置该动画过渡的百分比值(我不认为这是可能的,但是我会尽力确定是否可行)。

Any other way (possibly simpler) to have this working correctly is highly appreciated, thanks. 非常感谢您以其他方式(可能更简单)使此功能正常运行。

You can archive this type of behaviour using embedded view controller. 您可以使用嵌入式视图控制器来存档这种类型的行为。 See my view hierarchy in image : 在图像中查看我的视图层次结构: 在此处输入图片说明 You can embedded navigation controller as subview. 您可以将导航控制器嵌入为子视图。

In Last view you have to create view like navigation bar, and you have to write code for pop view controller. 在上一个视图中,您必须创建类似于导航栏的视图,并且必须为弹出视图控制器编写代码。

Here is the code : 这是代码:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

class SubViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
    }
    @IBAction func onClickBack(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Actual Result : 实际结果 :

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从弹出窗口将UIViewController推送到UINavigationController - Push UIViewController to UINavigationController from a popover 我可以从另一个 UIViewController 推送 UINavigationController - Can i Push UINavigationController From another UIViewController 推送没有UINavigationController的UIViewController - push UIViewController without UINavigationController UINavigationController推送到另一个UIViewController - UINavigationController push to a different UIViewController 如何用替换的 UINavigationController 推送 UIViewController - How to push UIViewController with replaced UINavigationController 从Storyboard嵌入到容器视图中的UINavigationController不会推送UIViewController - UINavigationController embedded in container view from Storyboard won't push UIViewController 从UINavigationController推送时,UIViewController的底部视图闪烁 - Bottom view in UIViewController flickering when push from UINavigationController 从UINavigationController展开到UIViewController - Unwind from UINavigationController to UIViewController 从UIViewController呈现UINavigationController - Present UINavigationController from UIViewController 从 uinavigationcontroller 中查找 uiviewcontroller - Find a uiviewcontroller from uinavigationcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM