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

    }

}

效果圖如下

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM