【问题标题】:customize UIBarButtonItem initWithBarButtonSystemItem to show up as grey not white in iOS 5自定义 UIBarButtonItem init 与 BarButtonSystemItem 在 iOS 5 中显示为灰色而不是白色
【发布时间】:2013-11-12 17:15:20
【问题描述】:

我在 UINavigationBar 上使用白色背景和 darkGrayColor 文本

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"uinavigationcontrollerbackground"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,UITextAttributeFont ,[UIColor darkGrayColor], UITextAttributeTextColor,  [UIColor whiteColor], UITextAttributeTextShadowColor, nil]];

这看起来不错,但我在整个应用程序中都有这样定义的按钮:

self.searchButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchProducts)];

我取消了默认的 blakc 背景外观:

[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"uibarbuttonbackground"]  forState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];

这只是一个白色方块。

现在我的图标仍然是白色的,带有浅灰色阴影,是否要更改这些默认图标?

【问题讨论】:

    标签: iphone ipad uibarbuttonitem ios5.1


    【解决方案1】:

    一旦我尝试更改 UIBarButtonSystemItemCamera 的背景,但没有任何效果。我猜这些默认的系统按钮类型 (UIBarButtonSystemItem ) 已经带有背景图片,所以改变它是行不通的。

    我建议您创建一个自定义视图并使用它来初始化您的栏按钮。不是一个完美的代码,但我希望它有所帮助。

     UIImage *image = [UIImage imageNamed:@"icon.png"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
        button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
        [button addTarget:self action:@selector(takePicture:)    forControlEvents:UIControlEventTouchUpInside];
    
        UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
        [v addSubview:button];
    
    
        UIBarButtonItem *barItem = [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];
    

    【讨论】:

    • 谁还在发布东西?我们在 iOS8,醒醒吧?
    • @emotality 是的,但这是从 12 月开始的,我在“还没有 ARC”的项目中使用了这段代码。我现在差不多醒了:)
    【解决方案2】:

    我使用了此处找到的类别解决方案,效果很好:

    UIBarButtonItem with custom image and no border

    我的工作比我希望的要多,但这是一个不错的干净解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多