Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将图像添加到导航栏_Iphone_Uitableview_Iphone Sdk 3.0_Uiimageview_Uinavigationbar - Fatal编程技术网

Iphone 将图像添加到导航栏

Iphone 将图像添加到导航栏,iphone,uitableview,iphone-sdk-3.0,uiimageview,uinavigationbar,Iphone,Uitableview,Iphone Sdk 3.0,Uiimageview,Uinavigationbar,我想要一个图像占据整个导航栏。这是基于导航的应用程序附带的导航。它与附带的UITableView一起出现在RootViewController上。我已经看到了一些这样做的例子 设置导航栏标题: UIImage *image = [UIImage imageNamed:@"TableviewCellLightBlue.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [self.navigation

我想要一个图像占据整个导航栏。这是基于导航的应用程序附带的导航。它与附带的UITableView一起出现在RootViewController上。我已经看到了一些这样做的例子

设置导航栏标题:

UIImage *image = [UIImage imageNamed:@"TableviewCellLightBlue.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar.topItem setTitleView:imageView];
问题是它只覆盖标题,而不是整个导航栏

还有一个线程:。最后,解决方案看起来使用了一个选项卡栏,我没有使用它。设置导航栏背景有那么复杂吗?还有其他更简单的方法吗

我希望有一个导航的背景,并且仍然能够使用标题文本。

在你的情况下,会很好

将“CustomImage”类别添加到UINavigationBar中, 然后你可以打电话:

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"yourNavBarBackground.png"];
[navBar setBackgroundImage:image];
此代码应该放在方法中

- (void)viewWillAppear:(BOOL)animated
要在其中具有自定义图像的视图控制器的。 在这种情况下,你最好打电话:

[navBar clearBackgroundImage]; // Clear any previously added background image

在setBackgroundImage之前(否则会多次添加…

实际上有一种更简单的方法可以将背景图像添加到任何
UIView
类或子类。它不需要类分类或扩展(子类化),您可以在“根据需要”的基础上这样做。例如,要将背景图像添加到视图控制器的导航栏,请执行以下操作:

self.navigationController.navigationBar.layer.contents = (id)[UIImage 
    imageNamed:@"background.png"].CGImage;

您需要记住将Quartz Core框架添加到您的项目中,并在需要的地方添加
#import
。这是一种更干净、更简单的方法,可以更改从
UIView
继承的任何图形层。当然,如果您希望为所有导航栏或选项卡栏实现类似的效果,那么子类化是有意义的。

我使用了cmp的解决方案,并添加了一些逻辑来删除它,因为我只希望在主屏幕上显示一个自定义背景图像

UIImage *image = [UIImage imageNamed:@"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

[self.navigationController.navigationBar addSubview:imageView];
HomeViewController.m

UIImage *image = [UIImage imageNamed:@"HomeTitleBG.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tag = 10;

UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];

if ( testImgView != nil )
{
    NSLog(@"%s yes there is a bg image so remove it then add it so it doesn't double it", __FUNCTION__);
    [testImgView removeFromSuperview];

} else {
    NSLog(@"%s no there isn't a bg image so add it ", __FUNCTION__);
}

[self.navigationController.navigationBar addSubview:imageView];


[imageView release];
UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];

if ( testImgView != nil )
{
    NSLog(@"%s yes there is a bg image so remove it", __FUNCTION__);
    [testImgView removeFromSuperview];  
} 
我还尝试使用建议的clearBackgroundImage方法,但无法使其工作,因此我给了图像一个标记,然后在视图上的其他ViewController中将其删除

OtherViewController.m

UIImage *image = [UIImage imageNamed:@"HomeTitleBG.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tag = 10;

UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];

if ( testImgView != nil )
{
    NSLog(@"%s yes there is a bg image so remove it then add it so it doesn't double it", __FUNCTION__);
    [testImgView removeFromSuperview];

} else {
    NSLog(@"%s no there isn't a bg image so add it ", __FUNCTION__);
}

[self.navigationController.navigationBar addSubview:imageView];


[imageView release];
UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];

if ( testImgView != nil )
{
    NSLog(@"%s yes there is a bg image so remove it", __FUNCTION__);
    [testImgView removeFromSuperview];  
} 

`

为使其在ios 6中工作,对ios 6进行了更改,请使用:

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
只要加上这一行

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];

砰!一行完成。

只需转到视图控制器并粘贴到super viewdidload中 并替换mainlogo中的图像,然后在set your image logo中设置导航标题

  - (void)viewDidLoad
{
   [super viewDidLoad];

   //set your image frame
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,70,45)] ;

  //set your image logo replace to the main-logo
   [image setImage:[UIImage imageNamed:@"main-logo"]];

   [self.navigationController.navigationBar.topItem setTitleView:image];

}

在appdelegate中添加代码未使用启动方法完成

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
else
{
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    // Uncomment to assign a custom backgroung image
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];

    // Uncomment to change the back indicator image
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

    [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]];

    // Uncomment to change the font style of the title

    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    shadow.shadowOffset = CGSizeMake(0, 0);

    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"HelveticaNeue-Bold" size:17], NSFontAttributeName, nil]];

    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
#定义UIColorFromRGB(rgbValue)[UIColorWithred:((float)((rgbValue&0xFF0000)>>16))/255.0绿色:((float)((rgbValue&0xFF00)>>8))/255.0蓝色:((float)(rgbValue&0xFF))/255.0 alpha:1.0]
如果([[[UIDevice currentDevice]系统版本]浮点值]<7.0)
{
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“navigation_or.png”]forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar外观]setTitleVerticalPositionAdjustment:0.0 for BarMetrics:UIBarMetricsDefault];
}
其他的
{
[[UINavigationBar外观]设置项颜色:UIColorFromRGB(0x067AB5)];
[[UINavigationBar外观]setTintColor:[UIColor whiteColor]];
//取消注释以指定自定义背景图像
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“navigation_or.png”]forBarMetrics:UIBarMetricsDefault];
//取消注释以更改后指示器图像
[[UINavigationBar外观]设置阴影图像:[[UIImage alloc]init]];
[[UINavigationBar外观]setBackgroundColor:[UIColor whiteColor]];
[[UINavigationBar外观]收件指示符号转换图像:[UIImage ImageName:@”“];
//取消注释以更改标题的字体样式
NSShadow*shadow=[[NSShadow alloc]init];
shadow.shadowColor=[UIColor colorWithRed:0.0绿色:0.0蓝色:0.0 alpha:0.8];
shadow.shadowOffset=CGSizeMake(0,0);
[[UINavigationBar外观]setTitleTextAttributes:[NSDictionary Dictionary With Objects and Keys:[UIColor ColorWith红色:245.0/255.0绿色:245.0/255.0蓝色:245.0/255.0 alpha:1.0],NSForegroundColorAttributeName,shadow,NSShadowAttributeName,[UIFont FontWith名称:@“HelveticaNeue Bold”大小:17],NSFontAttributeName,nil];
[[UINavigationBar外观]setTitleVerticalPositionAdjustment:0.0 for BarMetrics:UIBarMetricsDefault];
}

完美。谢谢另外,你知道如何设置标题栏的颜色吗?我尝试过这个,但它不起作用:[self.navigationController.navigationBar.titleView-setBackgroundColor:[UIColor-blueColor]];我也尝试过[navBar setBackgroundColor:[UIColor blueColor];有一个tintColor属性:尝试navBar.tintColor=[UIColor blueColor];如果我不使用setBackgroundImage部分,标题文本的颜色仍然不会随tintColor或BackgroundColor而改变。我猜我们在错误的层面上更改了特定的文本。这对其他答案有何改进?@yogesh你知道答案,但我认为代码看起来很漂亮,带有一些注释/步骤。@标记您可以在super view didload中添加此内容and@yogeshyadav该注释没有添加任何内容,即注释重复代码,并且没有解释它-以及它与其他答案的区别是什么?它们是错误的还是效率较低?@标记此代码仅在屏幕上显示导航标题图像图标我解释了什么?