使用 iOS 7 SDK 构建我的应用会改变导航栏及其按钮的外观:
上图显示了在使用 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/