iphone - 自定义 NavigationBar 按钮在 iOS 7 中看起来不同

标签 iphone ios objective-c ipad user-interface

使用 iOS 7 SDK 构建我的应用会改变导航栏及其按钮的外观:

navbar comparison

上图显示了在使用 iOS 6 的设备上运行时的样子,下图显示了在使用 iOS 7 的设备上运行的相同应用。

导航栏是使用背景图片创建的:

UIImage *navigationBarBackgroundImage = [[UIImage imageNamed:@"MyTopNavigationBackground"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 4, 0)];

UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[MyNavigationController class], nil];
[bar setBackgroundImage:navigationBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
[bar setTintColor:[UIColor colorWithRed:0.17 green:0.62 blue:0.23 alpha:1.0]];

左栏按钮由以下人员创建:

- (UIBarButtonItem *)slideoverMenuBarButtonItem {
    return [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_button_icon_menu.png"]
                                        style:UIBarButtonItemStylePlain
                                       target:self
                                       action:@selector(slideoverMenu)];
}

我更关心按钮外观发生了什么。处理向 iOS 7 新外观过渡的“最佳做法”是什么?

最佳答案

导航栏背景:

您需要使用可伸缩图像来填充导航栏。因为你的图像看起来是一个相当简单的渐变,像这样的东西应该让你接近:

[[UINavigationBar appearance] setBackgroundImage:[navigationBarBackgroundImage stretchableImageWithLeftCapWidth:0 topCapHeight:0]];

你的背景图片变成了 1w x 64h png。

条形按钮图片:

使用 [UIImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

 UIImage *buttonImage = [UIImage imageNamed:@"bar_button_icon_menu.png"];
 return [[UIBarButtonItem alloc] initWithImage:[buttonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                            style:UIBarButtonItemStylePlain
                                           target:self
                                           action:@selector(slideoverMenu)];

}

由于默认行为是使用应用程序色调颜色绘制非透明图像像素,“始终原始”模式将防止这种情况发生。

关于iphone - 自定义 NavigationBar 按钮在 iOS 7 中看起来不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19038498/

相关文章:

iPhone RESTful Web 服务

iphone - PerformSegueWithIdentifier if 语句不起作用

iphone - NSXMLParser问题带有“&”(Ampersand)字符

objective-c - 数据密集型 View 的加载屏幕

ios - 委托(delegate)不能使用 present() 和 dismiss()

即使定义了 key NSPhotoLibraryUsageDescription,访问 iPhone 照片库时 iOS 应用程序也会崩溃

ios - 无法将我的 React native 应用部署到应用商店 (iOS)

ios - 将应用从付费转换为应用内购买?我如何找出哪些用户支付了费用?

objective-c - 日期格式帮助

ios - 我应该为不同的数据源重用导航和 TableView Controller 吗?