根据横向/纵向更改 UINavigationBar 背景?
Posted
技术标签:
【中文标题】根据横向/纵向更改 UINavigationBar 背景?【英文标题】:Changing the UINavigationBar background depending on landscape/portrait? 【发布时间】:2013-10-05 08:51:30 【问题描述】:我希望我的 UINavigationBar 在我的 SignatureViewController 中正确旋转。这是当前旋转时在纵向和横向模式下的外观:http://tinypic.com/view.php?pic=2w5573a&s=5#.Uk5kgYapWrg
如您所见,UINavigationBar 在横向时不会按宽度缩放。我的项目是这样构建的:
rootViewController(UINavgationController) ------模态推送-----> SignatureViewController
(portrait only) (portrait/landscape)
请注意,我没有使用普通推送到 SignatureViewController,我使用的是模态推送。以防万一我想提及这一点,因为我不确定它是否会以任何奇怪的方式影响结果?...我尝试了不同的方法来更改 NavigationBar 的背景,具体取决于它是横向还是纵向。我将在下面发布一些:
试试 nr1:不工作
- (void) viewWillAppear:(BOOL)animated
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"navbar25g.png"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
试试 nr2:不工作
- (void) viewWillAppear:(BOOL)animated
UIImage *navBarLandscape = [[UIImage imageNamed:@"navbar25g.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(x,x,x,x)];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance]setBackgroundImage:navBarLandscape forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
试试 nr3:不工作
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
UIView *v = [[UIView alloc]init];
int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
int w = [[UIScreen mainScreen] bounds].size.width;
int h = [[UIScreen mainScreen] bounds].size.height;
switch(a)
case 4:
v.frame = CGRectMake(0,20,w,h);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(@"willrotate:1");
break;
case 3:
v.frame = CGRectMake(-20,0,w-20,h+20);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(@"willrotate:2");
[self.view addSubview:v];
break;
case 2:
v.frame = CGRectMake(0,-20,w,h);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(@"willrotate:3");
break;
case 1:
v.frame = CGRectMake(20,0,w-20,h+20);
NSLog(@"willrotate:4");
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
请注意,在 try nr3 中,NSLog 可以工作,但它拒绝更改 UINavigationBar 的背景。那么,有什么想法我会从这里做什么? / 问候
【问题讨论】:
【参考方案1】:您能否尝试实现UINavigationBar
类并覆盖一个drawRect:
方法
它应该可以工作
@implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
UIImage *navimage;
//we will check if screen width is 320 then it will be potrait
if(self.frame.size.width == 320)
navimage = [UIImage imageNamed: @"Your Potrait nav bar image"];
else
navimage = [UIImage imageNamed: @"Your Landscape nav bar image"];
[navimage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
@end
希望对你有帮助...
只需对 nr1 和 nr2 试试这个
只需用这个更改最后一行代码..如果它工作与否...让我知道
self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; //UIViewAutoresizingFlexibleWidth
【讨论】:
以上是关于根据横向/纵向更改 UINavigationBar 背景?的主要内容,如果未能解决你的问题,请参考以下文章