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