Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

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

I had the following code that worked perfectly in iOS7.

[UIView animateWithDuration:0.5 animations:^(void) {
    self.view.alpha = 0.5;
    [self.navigationController.navigationBar setAlpha:0.3];

}]; //to make the background view controller semi-transparent
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
[rootViewController setModalPresentationStyle:UIModalPresentationCurrentContext];
OverlayViewController *ctlr = [storyBoard instantiateViewControllerWithIdentifier:@"OverlayViewController"];
//present the OverlayViewController
[self presentViewController:ctlr animated:YES completion:nil];

Then I had the following in viewWillAppear of the background view controller, to revert back its view to full opaque.

[UIView animateWithDuration:1.0 animations:^(void) {
    self.view.alpha = 1.0;
    [self.navigationController.view setAlpha:1.0];
}];

Now, with iOS8, the above code doesn't set the background to be semi-transparent. Black color surrounds the OverlayViewController.

I found online that using UIModalPresentationOverCurrentContext will give the expected behavior. It does actually, but the background view controller is never taken off the view hierarchy (edited to add reference to this behavior: https://developer.apple.com/documentation/uikit/uimodalpresentationstyle). So, the viewWillAppear is never called, and so the semi-transparency is never removed.

Obviously, I can resort to hacks like using NSNotificationCenter and fire off a notification when the OverlayViewController is removed, but it feels like a roundabout way to do what should be simple. Is there any other way in which I can gracefully achieve this?

Corollary questions:

1) If UIModalPresentationOverCurrentContext is the only way to achieve this, then I am wondering if I would be forced to put two versions of the code to get it working both in iOS7 and iOS8.

2) Obviously that new enum is not recognized in earlier versions of Xcode. So, should my team upgrade to Xcode 6 just to ensure they can run this code, even if the rest of their work is centered around iOS7 only? Or is there any way of telling the older version of Xcode to ignore the particular code block that is needed only for iOS8?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...