xcode 在 iphone 的 uinavigationbar 上使用图像或色调颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/901542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:34:15  来源:igfitidea点击:

using image or tint color on uinavigationbar in iphone?

iphonexcodeinterface-builder

提问by Rahul Vyas

how do i show a background image on a navigation bar or give tint color to the navigation bar in a native iphone application??

我如何在导航栏上显示背景图像或在原生 iphone 应用程序中为导航栏提供色彩?

采纳答案by dbachrach

Was looking for this a week ago. Found this over here discussions. apple. com/thread.jspa?threadID=1649012&tstart=0 (sorry won't let me post a real link).

一周前正在寻找这个。发现这个在这里讨论。苹果。com/thread.jspa?threadID=1649012&tstart=0(对不起,我不能发布真正的链接)。

-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag{
if(image == NULL){ //might be called with NULL argument
    return;
}
UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image];
aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
aTabBarBackground.tag = bgTag;
[self addSubview:aTabBarBackground];
[self sendSubviewToBack:aTabBarBackground];
[aTabBarBackground release];
}
/* input: The tag you chose to identify the view */
-(void)resetBackground:(NSInteger)bgTag {
    [self sendSubviewToBack:[self viewWithTag:bgTag]];
}

I made this as a category to UINavigationBar. To set it a background image for a UINavigationBar inside a UINavigationBarController, I did this:

我把它作为 UINavigationBar 的一个类别。为了在 UINavigationBarController 中为 UINavigationBar 设置背景图像,我这样做了:

[navigationControllerForChannels.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bar.png"] withTag:48151623];

I've had some buginess when updating the tab bar, so you'll want to call

我在更新标签栏时遇到了一些错误,所以你会想打电话

[self.navigationController.navigationBar resetBackground:48151623];

After any modifications to the bar.

修改后的吧。

回答by Dare2Dream

For iOS5 use the following lines of code:

对于 iOS5,使用以下代码行:

UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

For backward compatibility do a check to see if the navigation bar responds to setBackgroundImage:forBarMetrics:

为了向后兼容,请检查导航栏是否响应 setBackgroundImage:forBarMetrics:

More information on: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/

更多信息:http: //sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/

回答by digdog

This's how I did on iOS4:

这就是我在 iOS4 上的做法:

#import <QuartzCore/QuartzCore.h> // For .layer 

self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"navigationBarBackgroundImage"].CGImage;
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];

No need to switch subviews between z-orders (-exchangeSubviewAtIndex:withSubviewAtIndex:), both background image and tintColor set in one line of code, and works with @2x image too.

无需在 z-orders (-exchangeSubviewAtIndex:withSubviewAtIndex:) 之间切换子视图,背景图像和 tintColor 都在一行代码中设置,并且也适用于 @2x 图像。

回答by ppolak

You can override UINavigationBar drawRect. The code can be placed to appDelegate.m I've tested it and it's working on 3x and 4x iOS.

您可以覆盖 UINavigationBar drawRect。代码可以放在 appDelegate.m 我已经测试过了,它可以在 3x 和 4x iOS 上运行。

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor blackColor]; //tint color
    UIImage *img = [UIImage imageNamed: @"navBarBg.png"]; // your image
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.tintColor = color;
 }@end

回答by Ashoor

For the iOS5 and iOS6 I've used this solutions and it worked perfectly, Making a Universal UINavigationBar Background Image.

对于 iOS5 和 iOS6,我使用了这个解决方案并且它完美地工作,制作通用 UINavigationBar 背景图像。

iPhone Retina Portrait 640px x 88px / iPhone Non-Retina Portrait 320px x 44px

iPhone Retina 人像 640px x 88px / iPhone Non-Retina 人像 320px x 44px

Inside AppDelegate.m

AppDelegate.m 内部

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Place this code

放置此代码

// Set the status bar to black color.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];

// Change @"menubar.png" to the file name of your image.
UIImage *navBar = [UIImage imageNamed:@"menubar.png"];

[[UINavigationBar appearance] setBackgroundImage:navBar forBarMetrics:UIBarMetricsDefault];

Don't forget to change the image name (menubar.png)

不要忘记更改图像名称(menubar.png)

Check out this link for the full answer http://www.lwxted.com/blog/2012/add-custom-background-image-uinavigationbar-ios-5/

查看此链接以获取完整答案http://www.lwxted.com/blog/2012/add-custom-background-image-uinavigationbar-ios-5/

回答by Ben Gottlieb

a background image is going to take a bit more work (you might want to try setting a titleView that's the same size as the bar itself; I haven't tried this myself) or adding a view behind existing subviews. Tint color is easy: navBar.tintColor = [UIColor orangeColor];

背景图像需要做更多的工作(您可能想尝试设置一个与栏本身大小相同的 titleView;我自己还没有尝试过)或在现有子视图后面添加一个视图。着色很容易: navBar.tintColor = [UIColor orangeColor];

回答by loretoparisi

If you use the CGImage solution, you may have a problem with image size:

如果使用 CGImage 解决方案,可能会遇到图像大小的问题:

CGRect layer=self.navigationController.navigationBar.frame;
layer.size.height=57.0;
layer.origin.y=0;
self.navigationController.navigationBar.frame=layer;
CGImageRef imageRef         = [UIImage imageNamed:@"myImg.png"].CGImage;
self.navigationController.navigationBar.layer.contents = (id)imageRef;

It seems to me that the image is streched down, since the layer seems to have a height of 44.0 pixel, but a background image for the UINavigationBar should be at least 57.0.

在我看来,图像被拉伸了,因为该层的高度似乎为 44.0 像素,但 UINavigationBar 的背景图像应该至少为 57.0。

If you try to move the layer's frame, all the buttons will move within it.

如果您尝试移动图层的框架,则所有按钮都将在其中移动。