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

Ste*_*den 3 iphone user-interface objective-c ipad ios

使用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]];
Run Code Online (Sandbox Code Playgroud)

左侧栏按钮由以下内容创建:

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

我更关心按钮外观发生了什么.处理这种向新iOS 7外观过渡的"最佳实践"是什么?

CSm*_*ith 5

导航栏背景:

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

[[UINavigationBar appearance] setBackgroundImage:[navigationBarBackgroundImage stretchableImageWithLeftCapWidth:0 topCapHeight:0]];
Run Code Online (Sandbox Code Playgroud)

并且您的背景图像变为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)];

}
Run Code Online (Sandbox Code Playgroud)

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