微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS 7中的UIBarButtonItem外观问题,这可能是Apple的bug吗?

我前段时间看到一篇文章,它在这里

User Interface Customization in iOS 6

显示了iOS 6的自定义.由于我撰写了使用该技术的应用程序的文章,它非常简单,没有任何魔力.

但是,我需要更新我的一个应用程序,在iOS 7下它无法正常工作.看来,UIBarButtonItems的自定义在第一次显示视图时不起作用.如果我解雇视图然后呈现它agin一切正常.看到的内容如下所示:

第一次观看:

第二次:

我在他的例子中看到了这个问题,我的代码和我写的测试应用程序.代码如下:

// Customizing the Back Bar Buttons
UIImage * btBack_30 = [[UIImage imageNamed:@"btBack_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,13,5)];
UIImage * btBack_24 = [[UIImage imageNamed:@"btBack_24"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,12,5)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:btBack_30 forState:UIControlStatenormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:btBack_24 forState:UIControlStatenormal barMetrics:UIBarMetricslandscapePhone];

你可以看到没有真正的魔法,非常标准,但我找不到任何理由或解释为什么这在iOS 7中不起作用.代码在 – (BOOL)应用程序中执行:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.

我希望有人看到这个,并提供解决方案.谢谢你的帮助!

**注意:有人提出这不是苹果虫,而是设计.我并不是说这是一个苹果问题,它可能更像是我的问题,但是如果你运行任何一个样本或者复制并粘贴下面的代码,很明显它第一次不能正常工作,而后续工作也是如此.这会让我相信api调用是有效的,但要么他们有错误,要么我错过了需要做的事情.

****更新4:**

我根据FruityGeek的建议将MyAppDelegate的init方法中的代码更改为以下内容,但仍然没有运气:

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        //Other UIAppearance proxy calls go here

        [[UIBarButtonItem appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:
          //[UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],UITextAttributeTextColor,//[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0],[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],UITextAttributeTextShadowColor,[NSValue valueWithUIOffset:UIOffsetMake(0.5,0.5)],UITextAttributeTextShadowOffset,[UIFont systemFontOfSize:12.0],UITextAttributeFont,nil]
                                                    forState:UIControlStatenormal];

        // Customizing the Back Bar Buttons
        //ios6 uses whole button background image
        UIImage * btBack_30 = [[UIImage imageNamed:@"btBack_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5)];
        UIImage * btBack_24 = [[UIImage imageNamed:@"btBack_24"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5)];
        [[UIBarButtonItem appearance] setBackButtonBackgroundImage:btBack_30 forState:UIControlStatenormal barMetrics:UIBarMetricsDefault];
        [[UIBarButtonItem appearance] setBackButtonBackgroundImage:btBack_24 forState:UIControlStatenormal barMetrics:UIBarMetricslandscapePhone];

        if ([[UIDevice currentDevice].systemVersion integerValue] >= 7)
        {
            //ios7 needs additional chevron replacement image
            UIImage * chevronReplacement = chevronReplacement = [btBack_30 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UIImage * chevronTransitionMaskReplacement = chevronTransitionMaskReplacement = [btBack_30 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [[UINavigationBar appearance] setBackIndicatorImage:chevronReplacement];
            [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:chevronTransitionMaskReplacement];
        }
    }
    return self;
}

****更新3:**

我已经为示例项目添加一个dropBox链接.这是上面已经发布的链接的补充,这是一个简单的应用程序,也显示了该问题.两者都可以在iOS 6和iOS 7的模拟器中构建和运行.在iOS 6的情况下,一切都按预期工作.在iOS 7中,如果单击表格单元格并显示一个视图,则不会显示自定义后退按钮,如果您返回并再次显示该按钮,则会显示该按钮.

我已经愚弄了好几天,所以我希望别人看到它并告诉我我错过了什么.

https://www.dropbox.com/s/oi1bh3emvtbmms0/NavigationBarDemo.zip

这可能很愚蠢,但它可能与我的图像有关吗?我将尝试使用不同的图像并发布更新.

>尝试使用不同的图像并没有区别,也使用了上面发布的样本中的图像.这是一个很长的镜头,但由于似乎没有人有更好的想法,但值得一试.

****更新2:**

我在另一个测试应用程序中尝试了这个并将代码移动到init
应用程序委托,它仍然无法正常工作.我有这个
发布在这里以及原始作者网站的链接
最佳.再加上一个论坛,但似乎没有人有解决方案.

我想知道这可能是一个苹果虫?

****更新1:**

代码从didFinishLaunchingWithOptions移动到willFinishLaunchingWithOptions和init,它似乎仍然不起作用.

***** INIT METHOD FROM AppDelegate.m

- (id)init
{
    // Create resizable images
    UIImage *gradientimage44 = [[UIImage imageNamed:@"navBar_44"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,0)];
    UIImage *gradientimage32 = [[UIImage imageNamed:@"navBar_32"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,0)];

    // Set the background image for *all* UINavigationBars
    [[UINavigationBar appearance] setBackgroundImage:gradientimage44 forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:gradientimage32 forBarMetrics:UIBarMetricslandscapePhone];

    // Customize the title text for *all* UINavigationBars
    [[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],[NSValue valueWithUIOffset:UIOffsetMake(1,1)],[UIFont boldSystemFontOfSize:18.0],nil]];

    // Customizing the NavBar Buttons
    UIImage * button30 = [[UIImage imageNamed:@"btButton_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5,5)];
    UIImage * button24 = [[UIImage imageNamed:@"btButton_24"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5)];
    [[UIBarButtonItem appearance] setBackgroundImage:button30 forState:UIControlStatenormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackgroundImage:button24 forState:UIControlStatenormal barMetrics:UIBarMetricslandscapePhone];

    [[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];

    [[UIBarButtonItem appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      //[UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],nil]
                                                forState:UIControlStatenormal];

    // Customizing the Back Bar Buttons
    UIImage * btBack_30 = [[UIImage imageNamed:@"btBack_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5)];
    UIImage * btBack_24 = [[UIImage imageNamed:@"btBack_24"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:btBack_30 forState:UIControlStatenormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:btBack_24 forState:UIControlStatenormal barMetrics:UIBarMetricslandscapePhone];

    return [super init];
}

解决方法

它确实看起来像一个Apple bug,你应该在bug报告器上输入一个bug.也就是说,我可以给你一个相对无痛的解决方法:将此代码添加到RecipetTableViewController:
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Recipe Book";

    UIBarButtonItem *it = [[UIBarButtonItem alloc] initWithTitle:self.title style:UIBarButtonItemStylePlain target:nil action:NULL];
    UIImage * btBack_30 = [[UIImage imageNamed:@"btBack_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5)];
    [it setBackButtonBackgroundImage:btBack_30 forState:UIControlStatenormal barMetrics:UIBarMetricsDefault];
    self.navigationItem.backBarButtonItem = it;
}

编辑:你可以重复这个错误,如果感兴趣,引用它的bug越多苹果将修复它:

BUG: 15506447

State:OpenProduct:iOS

19-Nov-2013 03:53 PM

Summary: Setting the UIBarButtonItem appearance proxy for the back bar
button item does not have any affect until the second appearance of
the button.

Steps to Reproduce: In appDelegate,before anything has appeared,add
these statements:

UIImage * gradientimage44 = [[UIImage imageNamed:@”navBar_44″]
resizableImageWithCapInsets:UIEdgeInsetsMake(0,0)]; UIImage *
gradientimage32 = [[UIImage imageNamed:@”navBar_32″]
resizableImageWithCapInsets:UIEdgeInsetsMake(0,0)]; // Set
the background image for all UINavigationBars [[UINavigationBar
appearance] setBackgroundImage:gradientimage44
forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance]
setBackgroundImage:gradientimage32
forBarMetrics:UIBarMetricslandscapePhone];

Expected Results: When a viewController is first pushed,its back
button has the image in it.

Actual Results: First time it appears,there is no image. Push the
view again and its there. Actually it does appear when you click on
the button the very first time,but not when the button first appears.

Version: Xcode 5.0.1,iOS 7.0.3

Notes: Adding this in the root view controller of the navigation
controller makes it work:

  • (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @”Recipe Book”; UIBarButtonItem *it = [[UIBarButtonItem alloc] initWithTitle:self.title
    style:UIBarButtonItemStylePlain target:nil action:NULL]; UIImage *
    btBack_30 = [[UIImage imageNamed:@”btBack_30″]
    resizableImageWithCapInsets:UIEdgeInsetsMake(0,5)]; [it
    setBackButtonBackgroundImage:btBack_30 forState:UIControlStatenormal
    barMetrics:UIBarMetricsDefault];
    self.navigationItem.backBarButtonItem = it; }

Attached demo project shows the problem.

Configuration:

Attachments: ‘DynamicsCatalog.zip’ was successfully uploaded.

编辑:我很高兴再次说,在bug记者中输入错误,有时确实有效!

原文地址:https://www.jb51.cc/iOS/333536.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐