简体   繁体   English

iOS 7 Nav Bar SKStoreProductViewController

[英]iOS 7 Nav Bar SKStoreProductViewController

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. 在iOS 6中,引入了SKStoreProductViewController以在应用程序中显示iTunes Store项目,因此用户无需离开应用程序即可查看它们。

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. 在iOS 6中,它是带有灰色书写的黑色,而在iOS 7中,它是带有黑色书写的白色。

Is there any way to change the navigation bar's tint color? 有没有办法改变导航栏的色调? (In iOS 6 & iOS 7) (在iOS 6和iOS 7中)

Thanks. 谢谢。

Not the nicest solution but you can use UINavigationBar's UIAppearance method to set the colour just before you show it: 不是最好的解决方案,但您可以使用UINavigationBar的UIAppearance方法在显示之前设置颜色:

[[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... 然后在SKStoreProductViewControllerDelegate方法中,将其更改回以前的任何内容...

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

Works for me. 适合我。

Unfortunately, no. 抱歉不行。 SKStoreProductViewController is a remote view controller , meaning its view is completely owned by another process and inaccessible programatically. SKStoreProductViewController是一个远程视图控制器 ,意味着它的视图完全由另一个进程拥有,并且以编程方式无法访问。

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. _UIRemoteView指示视图的内容托管在另一个进程中。

I had this problem inside Appirater rateApp function, I fixed it this way: 我在Appirater rateApp函数中遇到了这个问题,我这样解决了:

    //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 { (...)

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. 我发现我的UIAppearance设置正在改变文本颜色。

Here's a SKStoreProductViewController subclass that sets the bar text color to default, then restores your UIAppearance settings on dismiss. 这是一个SKStoreProductViewController子类,它将条形文本颜色设置为默认值,然后在关闭时恢复UIAppearance设置。 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. 致Kiran Panesar的技术。

I had this problem as well, and the answers given for the appearance code wasn't working for me. 我也有这个问题,并且appearance代码的答案对我不起作用。 I found that this is what fixed it, because I had changed my app-wide window.tintColor in the appDelegate earlier: 我发现,这是固定的,因为我改变了我的应用范围window.tintColorappDelegate更早版本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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