iPhone 下不支持 UIPopoverController,而使用 presentViewController:animated:completion: 方法得到的效果会全屏显示。有时候我们只想在部分区域类弹出视图,点击视图外区域自动消失。这里提供两种方案。
方案1:
1)增加 UIWindow,在新的窗口中添加视图控制器。
window =UIWindow(frame: UIScreen.mainScreen().bounds)
window.windowLevel =UIWindowLevelNormal
window.backgroundColor =UIColor.clearColor()
window.rootViewController =FakeViewController()
2)增加背景视图,用于响应消息
dimBackground = UIView(frame: UIScreen.mainScreen().bounds)
dimBackground.backgroundColor =UIColor.clearColor()
let gr:UITapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("dismiss"))
dimBackground.addGestureRecognizer(gr)
window.rootViewController!.view.addSubview(dimBackground)
3)创建actionSheet视图(这里省略),用于显示,为了得到移动的动画效果,先将其移出屏幕
var tmpFrame: CGRect = self.actionSheet.frame
tmpFrame.origin.y =UIScreen.mainScreen().bounds.size.height
actionSheet.frame = tmpFrame;
window.rootViewController!.view.addSubview(actionSheet)
4)从下往上移动到屏幕之内
self.window.hidden =false
UIView.animateWithDuration(0.2, animations:{
self.dimBackground.backgroundColor =UIColor(white: 0.0, alpha:0.2)
let frame: CGRect = UIScreen.mainScreen().bounds
self.actionSheet.frame =CGRectMake(0, frame.size.height-self.actionSheet.frame.size.height,self.actionSheet.frame.size.width,self.actionSheet.frame.size.height);
})
5)点击背景视图响应消息,销毁窗口(具体可根据要求,或者保存,下次需要再次移到屏幕内即可)
func dismiss() {
UIView.animateWithDuration(0.2, animations: {[weak weakSelf =self] () -> Voidin
self.dimBackground.backgroundColor =UIColor.clearColor()
let frame: CGRect = UIScreen.mainScreen().bounds
self.actionSheet.frame =CGRectMake(0, frame.size.height,self.actionSheet.frame.size.width,self.actionSheet.frame.size.height);
}) { (complete) -> Void in
self.window =nil
}
}
方案2:
1)创建要显示的 UIViewController
var frame:CGRect = self.view.bounds
frame.origin.y = frame.size.height - 44.0
frame.size.height =44.0
let pageJumpController = PageJumpController(frame: frame, totolPage: (currentPageItem!.totalPage), delegate:self)
2)加入视图到上层视图上,并将视图控制器加入其栈中
self.view.addSubview(pageJumpController.view)
self.addChildViewController(pageJumpController);
pageJumpController.didMoveToParentViewController(self)
3)不想显示时,移出栈即可。注意,如果只将子视图移除,其控制器仍然在栈中,被父控制器所有,不会被销毁。根据具体情况实现
self.view.removeFromSuperview()
self.removeFromParentViewController()