简体   繁体   中英

SKStoreProductViewController shows the status bar in iOS7

In iOS7, when a SKStoreProductViewController is presented, it shows the status bar, making it difficult to hit the [Cancel] button.

Can it be disabled?

In my app, UIViewControllerBasedStatusBarAppearance ("View controller-based status bar appearance") is set to YES.

Putting the following code anywhere in the project:

@interface SKStoreProductViewController (StatusBarFixing)

@end

@implementation SKStoreProductViewController (StatusBarFixing)

-(BOOL) prefersStatusBarHidden { return YES; }

@end

... seems to do the trick, even if it is a bit sketchy. However, it would only work if UIViewControllerBasedStatusBarAppearance is set to YES

It seems to me that the status bar is not meant to be shown here. In my opinion, it's best to just hide the status bar and everything will look like it's supposed to.

-(void)showAppInAppstore {
    SKStoreProductViewController *spvc = [[SKStoreProductViewController alloc] init];
    spvc.delegate = self;

    // ... start activity indicator here if you wish

    __weak typeof(self) weakSelf = self;

    [spvc loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier : @(APP_STOREID)}
                    completionBlock:^(BOOL result, NSError *error) {

                        // there is no way to stop this task so...
                        // make sure that user hasn't navigated away from "rate" screen
                        if(weakSelf != nil && weakSelf.isViewLoaded && weakSelf.view.window != nil) {

                            // ... stop activity indicator here

                            if(result == NO || error != nil) {

                                // handle error if needed

                            } else {

                                // Hide status bar
                                [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

                                // Present store controller
                                UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;                                
                                [controller presentViewController:spvc animated:YES completion:nil];
                            }                           
                        }
                    }
    ];
}

Now make sure that the status bar becomes visible once user is done

-(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [viewController dismissViewControllerAnimated:YES completion:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}                

You can subclass SKStoreProductViewController and control status bar appearance from that point. That's simple and elegant.

SKStoreProductViewControllerWithoutStatusBar.h

#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>

@interface SKStoreProductViewControllerWithoutStatusBar : SKStoreProductViewController

@end

SKStoreProductViewControllerWithoutStatusBar.m

import "SKStoreProductViewControllerWithoutStatusBar.h"

@interface SKStoreProductViewControllerWithoutStatusBar ()

@property (nonatomic) BOOL wasStatusBarHidden;

@end

@implementation SKStoreProductViewControllerWithoutStatusBar

// Works if UIViewControllerBasedStatusBarAppearance == NO

- (void)viewWillAppear:(BOOL)animated {
    self.wasStatusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarHidden:self.wasStatusBarHidden withAnimation:UIStatusBarAnimationSlide];
    [super viewWillDisappear:animated];
}

// Works if UIViewControllerBasedStatusBarAppearance == YES

- (BOOL)prefersStatusBarHidden {
    return YES;
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM