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];
}
}
效果圖以下