2012-07-09 56 views
1

我有我想创建一个自定义uinavigationn吧我想这码iphone:在导航栏设置背景图片是不恰当

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
//here for v, width= navBar width and height=navBar height 
//  
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"newsBanner.png"]]]; 
[self.navigationController.navigationBar addSubview:view]; 
表视图

的问题是图像不居中导致其大小为640 x 72 ...有没有办法让它合适?

回答

2

您的导航栏应该只有44像素高。最重要的是,它的最大宽度是320点(在视网膜设备上是640像素,在非高清设备上是320像素)。所以你的图片尺寸太大了。作出这样的具有320x44尺寸的图像,然后做到这一点:

UINavigationBar *theBar = self.navigationController.navigationBar; 
if ([theBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
    UIImage *image = [UIImage imageNamed:@"your image here"]; 
    [theBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
} 

当然,这只会在iOS 5的工作,你需要找到处理设置在操作系统后台低于5.0等方式进行。但是有很多堆栈溢出问题可以解决这个问题:)

+0

作为一种替代方案,您可能可以在代码中缩放图像,但这并不是一件有趣的事情,可能比仅仅将图像设置为正确的尺寸更麻烦。 – bgoers 2012-07-09 20:07:08

+0

当您向导航项添加提示时,图像会重复出现,并且我们无法将图像拉伸至导航栏,因此存在问题。 – djleop 2012-12-05 13:51:19