Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

ios - 在显示之前将ViewController加载到ContainerView中,因此看不到过渡(Load ViewController into ContainerView prior to showing so no transition is visible)

Using Swift 4+, iOS 11.4+, Xcode 10+ (使用Swift 4 +,iOS 11.4 +,Xcode 10+)

I'm using a ContainerView with a Navigation controller to load multiple ViewControllers. (我正在使用带有导航控制器的ContainerView来加载多个ViewController。)

In addition, the ViewController that holds the ContainerView is also pushed onto a different NavigationController stack. (另外,保存ContainerView的ViewController也被推送到另一个NavigationController堆栈上。)

See this graphic: (看这个图:)

在此处输入图片说明

The "Menu" is the root controller in the stack. (“菜单”是堆栈中的根控制器。) I am pushing another ViewController (TopViewController) onto the same stack - so far so good. (我将另一个ViewController(TopViewController)推送到同一堆栈上-到目前为止一切顺利。)

My issue is that I want to "pre-load" the TopViewController > ContainerView with a different ViewController (Item1) before the TopViewController becomes visible. (我的问题是,我想在TopViewController变得可见之前 ,使用其他ViewController(Item1)“预加载” TopViewController> ContainerView。)

There are other ViewControllers in addition Item1 that I will need to "pre-load" in this manner. (除了Item1之外,还有其他ViewController我需要以这种方式“预加载”。)

I have tried using "viewDidLoad" in TopViewController to push Item1, (我尝试在TopViewController中使用“ viewDidLoad”来推送Item1,)

toVC = storyboard?.instantiateViewController(withIdentifier: "Item1") as! Item1
TopNav.pushViewController(toVC, animated: true)

and it works - but only after TopViewController becomes visible. (并且有效-但仅在To??pViewController可见之后。) This means that you can see Item1 "pop" into view rather than already being visible. (这意味着您可以在视图中看到Item1“弹出”,而不是已经可见。)

I have also tried this code from within the Menu VC, but it does not push anything onto the ContainerView. (我也从Menu VC中尝试过此代码,但它不会将任何内容推送到ContainerView上。)

let toVC = self.storyboard?.instantiateViewController(withIdentifier: "TopViewController") as! TopViewController

// Tried this to 'pre-load' the next VC - shows no VC, and disables 'back' navigation
let toNav = toVC.navigationController
let nextVC = storyboard?.instantiateViewController(withIdentifier: "Item1") as! Item1
// Neither of the lines below loads the Item1 VC....
//toNav?.viewControllers = [nextVC]
toNav.pushViewController(toVC, animated: true)

How can I push Item1 onto the Navigation stack for the ContainerView before the VC with the ContainerView becomes visible? (在带有ContainerView的VC可见之前,如何将Item1推送到ContainerView的导航堆栈上?)

FYI: You can download my sample project here: https://gitlab.com/whoit/newviews (仅供参考:您可以在此处下载我的示例项目: https//gitlab.com/whoit/newviews)

  ask by wayneh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  TopNav.pushViewController(toVC, animated: true) 

and it works - but only after TopViewController becomes visible. (并且有效-但仅在To??pViewController可见之后。)

Because you asked for animation. (因为您要求动画。) Change animated: true to animated: false If that's what you want. (更改animated: trueanimated: false如果这就是您想要的。)


The preceding is right, but there were a lot of other issues with your project. (前面是对的,但是您的项目还有很多其他问题。)

Most important, your container view has no constraints . (最重要的是, 您的容器视图没有任何约束 。) This causes it to end up the wrong size. (这将导致其尺寸错误。) Give it constraints in the storyboard. (在情节提要中赋予它约束。)

Second, do what I said above: push toVC with false animation. (第二,做我上面说的:用false动画推toVC 。)

Third, you need to preload your top view controller's view, like this: (第三,您需要预加载顶视图控制器的视图,如下所示:)

@IBAction func Item1_Tap(_ sender: UIButton) {
    let toVC = self.storyboard?.instantiateViewController(
        withIdentifier: "TopViewController") as! TopViewController
    toVC.loadViewIfNeeded() // add this line
    // then delete all the other useless stuff

Result: (结果:)

在此处输入图片说明


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...