PresentViewController 如何不遮挡住原来的viewController界面呢?
可能有时候会遇到这种需求,需要弹出一个功能比较独立的视图实现一些功能,但是却不想单纯添加一个View上去,想做成viewController的形式。那么本文就详细说明下如何实现 presentViewController并且不覆盖原先视图的解决方案。
具体源码请访问 http://www.cnblogs.com/sely-ios/p/4552134.html
UIViewController之间的底部的跳转机制,具体内容太多就不详细说明了, 不过苹果提供了自定义UIViewController之间跳转的一个Delegate,那就是UIViewControllerTransitioningDelegate,具体请参照XCode中此Protocol的介绍
那么iOS7之前就需要自定义UIViewControllerTransitioningDelegate以及UIViewControllerAnimatedTransitioning 来完成我们的需求,iOS8之后苹果已经给出解决方案,只需要设置UIViewController 的 modalPresentationStyle 属性为 UIModalPresentationOverCurrentContext就可以轻松达到我们的要求。
具体如何实现呢?
这里我也参照了 github上由Blanche Faur贡献的Demo https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions,很轻松就能达到想要的效果了,下面是跳转的代码
- ( IBAction )presentViewController:( id )sender
{
if ( self . background )
{
[ self . view bringSubviewToFront : self . background ];
self . background . hidden = NO ;
self . background . layer . opacity = 0.3 ;
}
UINavigationController *navi = [ self . storyboardinstantiateViewControllerWithIdentifier : @"ToViewControllerNavi" ];
ToViewController *toViewController = navi. viewControllers [ 0 ];
[toViewController setHandler :^(){
self . background . hidden = YES ;
}];
if ([[[ UIDevice currentDevice ] systemVersion ] floatValue ] < 8.0 )
{
[navi setTransitioningDelegate : self . transDelegate ];
navi. modalPresentationStyle = UIModalPresentationCustom ;
[ self presentViewController :navi animated : YES completion : nil ];
}
else
{
toViewController. view . backgroundColor = [ UIColor clearColor ];
navi. modalPresentationStyle = UIModalPresentationOverCurrentContext ;
[ self presentViewController :navi animated : YES completion : nil ];
}
}
效果图如下


PresentViewController 如何不遮挡住原来的viewController界面呢?
可能有时候会遇到这种需求,需要弹出一个功能比较独立的视图实现一些功能,但是却不想单纯添加一个View上去,想做成viewController的形式。那么本文就详细说明下如何实现 presentViewController并且不覆盖原先视图的解决方案。
具体源码请访问 http://www.cnblogs.com/sely-ios/p/4552134.html
UIViewController之间的底部的跳转机制,具体内容太多就不详细说明了, 不过苹果提供了自定义UIViewController之间跳转的一个Delegate,那就是UIViewControllerTransitioningDelegate,具体请参照XCode中此Protocol的介绍
那么iOS7之前就需要自定义UIViewControllerTransitioningDelegate以及UIViewControllerAnimatedTransitioning 来完成我们的需求,iOS8之后苹果已经给出解决方案,只需要设置UIViewController 的 modalPresentationStyle 属性为 UIModalPresentationOverCurrentContext就可以轻松达到我们的要求。
具体如何实现呢?
这里我也参照了 github上由Blanche Faur贡献的Demo https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions,很轻松就能达到想要的效果了,下面是跳转的代码
- ( IBAction )presentViewController:( id )sender
{
if ( self . background )
{
[ self . view bringSubviewToFront : self . background ];
self . background . hidden = NO ;
self . background . layer . opacity = 0.3 ;
}
UINavigationController *navi = [ self . storyboardinstantiateViewControllerWithIdentifier : @"ToViewControllerNavi" ];
ToViewController *toViewController = navi. viewControllers [ 0 ];
[toViewController setHandler :^(){
self . background . hidden = YES ;
}];
if ([[[ UIDevice currentDevice ] systemVersion ] floatValue ] < 8.0 )
{
[navi setTransitioningDelegate : self . transDelegate ];
navi. modalPresentationStyle = UIModalPresentationCustom ;
[ self presentViewController :navi animated : YES completion : nil ];
}
else
{
toViewController. view . backgroundColor = [ UIColor clearColor ];
navi. modalPresentationStyle = UIModalPresentationOverCurrentContext ;
[ self presentViewController :navi animated : YES completion : nil ];
}
}
效果图如下