问题
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