iOS 7 Nav Bar SKStoreProductViewController

假装没事ソ 提交于 2019-12-22 05:43:34

问题


In iOS 6, SKStoreProductViewController was introduced to show iTunes Store items in apps, so the user would not have to leave the app to view them.

So far, I have not found a way to customize the navigation bar of this view controller. In iOS 6, it is black with grey writing, and in iOS 7, it is white with black writing.

Is there any way to change the navigation bar's tint color? (In iOS 6 & iOS 7)

Thanks.


回答1:


Not the nicest solution but you can use UINavigationBar's UIAppearance method to set the colour just before you show it:

[[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]];

SKStoreProductViewController *storeProductViewController = [[SKStoreProductViewController alloc] init];
[storeProductViewController setDelegate:self];
[self presentViewController:storeProductViewController animated:YES completion:nil];

And then in the SKStoreProductViewControllerDelegate method, change it back to whatever it was previously...

-(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

Works for me.




回答2:


Unfortunately, no. SKStoreProductViewController is a remote view controller, meaning its view is completely owned by another process and inaccessible programatically.

This can be confirmed by looking at the recursive description of the controller's view:

<UIView: 0x8d48da0; frame = (0 0; 320 480); layer = <CALayer: 0x8d48d70>>
   | <_UISizeTrackingView: 0x9b53700; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x9b53770>>
   |    | <_UIRemoteView: 0x9b51d70; frame = (0 0; 320 480); transform = [0.5, -0, 0, 0.5, -0, 0]; userInteractionEnabled = NO; layer = <CALayerHost: 0x9b55ae0>>

The _UIRemoteView indicates that the contents of the view is hosted in another process.




回答3:


I had this problem inside Appirater rateApp function, I fixed it this way:

    //Use the in-app StoreKit view if available (iOS 6) and imported. This works in the simulator.
    if (!_openInAppStore && NSStringFromClass([SKStoreProductViewController class]) != nil) {

        SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init];
        storeViewController.delegate = self.sharedInstance;

        NSNumber *appId = [NSNumber numberWithInteger:_appId.integerValue];
        [storeViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:appId} completionBlock:^(BOOL result, NSError *error) {
            [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

            id <AppiraterDelegate> delegate = self.sharedInstance.delegate;
            if ([delegate respondsToSelector:@selector(appiraterWillPresentModalView:animated:)]) {
                [delegate appiraterWillPresentModalView:self.sharedInstance animated:_usesAnimation];
            }


            [[self getRootViewController] presentViewController:storeViewController animated:_usesAnimation completion:^{
                [self setModalOpen:YES];
                //Temporarily use a black status bar to match the StoreKit view.
                [self setStatusBarStyle:[UIApplication sharedApplication].statusBarStyle];
                [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent animated:_usesAnimation];

            }];

        }];
    //Use the standard openUrl method if StoreKit is unavailable.
    } else { (...)



回答4:


You can't customize the bar's color, but its default colors look fine. I found that my UIAppearance settings were changing the text color.

Here's a SKStoreProductViewController subclass that sets the bar text color to default, then restores your UIAppearance settings on dismiss. Useful if you're presenting from multiple places in your code.

@interface GBStoreProductViewController ()
{
    UIColor *navBarTintColor;
    NSDictionary *navBarTitleTextAttributes;
}

@end

@implementation GBStoreProductViewController

- (id)init
{
    UIColor *tintColor = [[UINavigationBar appearance] tintColor];
    NSDictionary *titleTextAttributes = [[UINavigationBar appearance] titleTextAttributes];
    [[UINavigationBar appearance] setTintColor:nil];
    [[UINavigationBar appearance] setTitleTextAttributes:nil];
    if (self = [super init]) {
        navBarTintColor = tintColor;
        navBarTitleTextAttributes = titleTextAttributes;
    }
    return self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[UINavigationBar appearance] setTintColor:navBarTintColor];
    [[UINavigationBar appearance] setTitleTextAttributes:navBarTitleTextAttributes];
}

@end

Credits to Kiran Panesar for the technique.




回答5:


I had this problem as well, and the answers given for the appearance code wasn't working for me. I found that this is what fixed it, because I had changed my app-wide window.tintColor in the appDelegate earlier:

[[UIApplication sharedApplication] keyWindow].tintColor = nil;


来源:https://stackoverflow.com/questions/18838727/ios-7-nav-bar-skstoreproductviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!