presentViewController: 如何不覆蓋原先的 viewController介面,uipageviewcontroller
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.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];
}
}
如下