当屏幕切换到横向模式时,我想为导航栏设置更大的背景。这就是我在视图控制器中所做的:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
UIDeviceOrientation deviceOrientation = toInterfaceOrientation;
if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
[self.navigationController.navigationBar setBackgroundImage: [UIImageHelper createTopBar: deviceOrientation] forBarMetrics: UIBarMetricsDefault];
}
else
[self.navigationController.navigationBar setBackgroundColor: [UIColor colorWithPatternImage: [UIImageHelper createTopBar: [[UIDevice currentDevice] orientation]]]];
}
这是createTopBar
方法:
+ (UIImage *)createTopBar: (UIDeviceOrientation) orientation {
// Create a new image context
CGSize size;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
if ([UIDevice isiPhone5]) {
size = CGSizeMake(568, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 34), NO, 0.0);
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
size = CGSizeMake(1024, 44);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
}
else {
size = CGSizeMake(480, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 34), NO, 0.0);
}
}
else{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
size = CGSizeMake(768, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(768, 44), NO, 0.0);
}
else if ([UIDevice isiPhone5]) {
size = CGSizeMake(320, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
}
else {
size = CGSizeMake(320, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
}
}
UIImage * image = [UIImage imageNamed: @"top_bar_without_title"];
[image drawInRect:CGRectMake(0, 0, size.width, size.height+4)];
UIImage * destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
在纵向模式下结果很好:
这在iOS 6中的横向模式下也很有效:
但这就是iOS 7中横向模式的结果:
您可以看到状态栏与导航栏重叠,导航栏底部还有一些额外的空间。 (另一方面,我已经编辑了info.plist文件来修复重叠状态栏问题。这只有在我尝试为导航栏设置新的背景图像时才会发生。)你对这个问题有什么建议吗?如果你这样做,请告诉我,谢谢你。
答案 0 :(得分:2)
感谢@ eagle.dan.1349的回答,我想出了一个想法:我将导航栏背景的高度扩展到包含状态栏的高度,然后开始从低到高的绘制背景图像留下状态栏的空间:
+ (UIImage *)createTopBar: (UIDeviceOrientation) orientation {
// Create a new image context
CGSize size;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
if ([UIDevice isiPhone5]) {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
size = CGSizeMake(568, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 54), NO, 0.0);
}
else {
size = CGSizeMake(568, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 34), NO, 0.0);
}
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
size = CGSizeMake(1024, 44);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
}
else {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
size = CGSizeMake(480, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 54), NO, 0.0);
}
else {
size = CGSizeMake(480, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 34), NO, 0.0);
}
}
}
else{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
size = CGSizeMake(768, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(768, 44), NO, 0.0);
}
else if ([UIDevice isiPhone5]) {
size = CGSizeMake(320, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
}
else {
size = CGSizeMake(320, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
}
}
UIImage * image = [UIImage imageNamed: @"top_bar_without_title"];
if ((orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight) &&
[[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
[[UIColor blackColor] set];
UIRectFill(CGRectMake(0, 0, size.width, 40));
[image drawInRect:CGRectMake(0, 20, size.width, size.height+4)];
}
else {
[image drawInRect:CGRectMake(0, 0, size.width, size.height+4)];
}
UIImage * destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
瞧,它就像一个魅力!真的希望这对于那些试图像我一样坚持iOS 6的状态栏风格的人来说有点帮助。
答案 1 :(得分:1)
iOS 7在系统控制和外观方面有很多未记录的行为。我建议你“放弃”对抗状态栏并调整你的形象以适应它。您可以尝试在图像顶部添加20px的黑色。