使 UIViewController 从底部出现,然后隐藏它 [英] Make UIViewController appear from bottom, then hide it

查看:25
本文介绍了使 UIViewController 从底部出现,然后隐藏它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 UIViewController 从另一个视图控制器的底部出现.

I need a UIViewController to appear from the bottom of another view controller.

我还需要使用按钮再次隐藏它的选项,无需重新加载即可返回上一个视图控制器.

I also need the option to hide it again with a button, getting back to the previous view controller without reloading it.

有可能吗?

推荐答案

我建议你分别使用 presentViewControllerdismissViewController ,但使用这个,呈现的视图控制器将被释放关闭时,这意味着它需要重新加载.

I would suggest you use presentViewController and dismissViewController respectively but using this the presented view controller would be deallocated on dismiss which would mean it needs to be reloaded.

解决这个问题有一个有点hacky的方法,请允许我解释一下:

There is a somewhat hacky way around this, allow me to explain:

您可以从底部创建要显示的视图控制器的实例,然后将其添加为当前视图控制器的子项.然后将它添加到视图层次结构并从屏幕外设置动画.使用以下代码作为指导:

You can create an instance of the view controller you want to show from the bottom, then add it as a child of the current view controller. Then add it to the view heirarchy and animate it up from offscreen. Use the following code as a guide:

let vc = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController")
        self.addChildViewController(vc)
        self.view.addSubview(vc.view)
        vc.view.frame = CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: self.view.frame.height)
        vc.willMoveToParentViewController(self)
        UIView.animateWithDuration(1) { 
            vc.view.frame = self.view.frame
        }

然后只需反转动画即可在按下按钮时将其隐藏.

Then just reverse the animation to hide it on button press.

这篇关于使 UIViewController 从底部出现,然后隐藏它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆