Presenting semi-transparent viewcontroller that works both in iOS7 and iOS8

前端 未结 3 1324
花落未央
花落未央 2020-12-14 20:05

I had the following code that worked perfectly in iOS7.

[UIView animateWithDuration:0.5 animations:^(void) {
    self.view.alpha = 0.5;
    [self.navigation         


        
相关标签:
3条回答
  • 2020-12-14 20:46

    In Swift for iOS 8.x:

    setting UIModalPresentationStyle.OverCurrentContext to the viewController being presented does the job.

        func presentTransparentController(){
            var viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewControllerNamed") as! ViewController
    
            // set to .OverCurrentContext
            viewController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
    
            // presents the view controller as usual
            self.presentViewController(viewController, animated: true, completion: nil)
        }
    
    0 讨论(0)
  • 2020-12-14 20:56

    You have to handle two configurations for iOS 7 and iOS8. In both case you need to make sure that the background view controller is not taken off the view hierarchy. This can be accomplish with:

    1. On iOS7 by setting the modalPresentationStyle flag to UIModalPresentationCurrentContext for the presenting view controller. You need to identify who is the presenting view controller: it is not necessarily self.navigationController if you have multiple container view controllers.

    2. On iOS8 by setting the modalPresentationStyle flag to UIModalPresentationOverCurrentContext for the presented view controller. If you are using storyboard make sure the presentation style is set to Default for the storyboard segue and within the prepareForSegue method set the presentation style to the destination view controller to UIModalPresentationOverCurrentContext.

    Now to answer your corollary questions:

    1) You need to have code to handle both situation on iOS7 and iOS8:

    Define some macros to check the version number the application is running on. For example the following will do:

    #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
    #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
    

    2) Obviously if you have code that needs to be compiled on iOS7 and iOS8 so your team needs to update to the latest version of XCode" XCode 6.1 as of now.

    0 讨论(0)
  • 2020-12-14 20:58

    Based on @Tiguero's answer, I created a small category class to solve the problem.

    @implementation UIViewController (Extensions)
    
    - (void) presentTransparentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
    {
        if(SYSTEM_VERSION_LESS_THAN(@"8.0")) {
            self.parentViewController.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
        }else{
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        }
    
        [self presentViewController:viewControllerToPresent animated:YES completion:completion];
    }
    
    @end
    

    The iOS7 version required me to hardcode the controller that does the presenting.

    0 讨论(0)
提交回复
热议问题