iphone: setting background image in navigationbar is not fitting

被刻印的时光 ゝ 提交于 2019-12-24 07:18:17

问题


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];

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


回答1:


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];
}

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 :)



来源:https://stackoverflow.com/questions/11401768/iphone-setting-background-image-in-navigationbar-is-not-fitting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!