简体   繁体   English

iOS Swift弹出视图控制器

[英]iOS Swift Popup View Controller

I need to have a ViewController slide up, be centered, and overlay the current ViewController. 我需要让ViewController向上滑动,居中并覆盖当前的ViewController。 I thought the following code would work but it does not: 我认为以下代码可以工作,但它没有:

let view = storyboard?.instantiateViewControllerWithIdentifier("castSpell") as! CastSpellViewController
view.modalPresentationStyle = .OverCurrentContext
presentViewController(view, animated: true, completion: nil)

The new ViewController is shown but just as a full screen view. 显示了新的ViewController,但就像全屏视图一样。 I have it's size set to 400x300 in the storyboard. 我在故事板中将它的大小设置为400x300。 I know I can do the same thing by having a view within a view and then showing the overlay view. 我知道我可以通过在视图中显示视图然后显示叠加视图来做同样的事情。 Is that the route I need to go? 这是我需要去的路线吗?

You need to set the presentation style on the presenter , not the presenting view. 您需要设置呈现样式的主持人 ,而不是呈现视图。

Use self.modalPresentationStyle = .OverCurrentContext 使用self.modalPresentationStyle = .OverCurrentContext

let popOverVC = UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewControllerWithIdentifier("ViewControllerName") as! PopOverViewContoller
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.containerView.frame
self.containerView.addSubview(popOverVC.view)
popOverVC.didMoveToParentViewController(self)

Use the above code to add viewcontroller as a popover view. 使用上面的代码将viewcontroller添加为弹出视图。 The container view is optional. 容器视图是可选的。 I like using container views when the subviews position is fixed. 当子视图位置固定时,我喜欢使用容器视图。 I can position the container using autolayout and keep it hidden until needed. 我可以使用autolayout定位容器,并在需要之前将其隐藏起来。

In you case, you can place the container/subView at the bottom of the screen from where you need to slide up the subview. 在这种情况下,您可以将容器/子视图放在屏幕底部,从而需要向上滑动子视图。

UIView.animateWithDuration(0.5) { () -> Void in
     //Animate here
}

Then change the frame of the container/subview inside the above block of code. 然后在上面的代码块中更改容器/子视图的框架。

Note : Container view is just a normal UIView. 注意:容器视图只是一个普通的UIView。 I like naming it container :) 我喜欢命名容器:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM