presentViewController: 如何不覆盖原先的 viewController界面

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 ];

}

}

效果图如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值