iPhone:在导航栏中设置背景图片不合适 [英] iphone: setting background image in navigationbar is not fitting

查看:56
本文介绍了iPhone:在导航栏中设置背景图片不合适的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,我想制作一个我尝试过此代码的自定义uinavigationn栏

i have a table view that i want to make a custom uinavigationn bar i tried this code

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 ...是否有合适的方法?

the problem is the image is not centered cause its size is 640 x 72... is there a way to make it fit?

推荐答案

您的导航栏应该只有44像素高.最重要的是,最大宽度为320点(在视网膜设备上为640像素,在非高清设备上为320像素).因此,您的图片太大了.制作尺寸为320x44的图像,然后执行以下操作:

Your nav bar should only be 44 pixels tall. On top of that, it is maximum 320 points wide (640 px on retina devices, 320 on non-HD devices). So the size of your image is way too big. Make an image that has 320x44 dimensions, and then do this:

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

当然,这仅适用于iOS5.您将需要找到其他方法来处理将操作系统设置为低于5.0的背景.但是有很多堆栈溢出问题可以解决这个问题:)

Of course that will only work on iOS 5. You'll need to find other ways of handling setting the background on OS below 5.0. But there are plenty of stack overflow questions that address that issue :)

这篇关于iPhone:在导航栏中设置背景图片不合适的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆