我想在导航栏中添加一张图片。它应该遍布我的整个应用程序,且应添加到所有导航控制器中。
任何帮助将不胜感激。
我想在导航栏中添加一张图片。它应该遍布我的整个应用程序,且应添加到所有导航控制器中。
任何帮助将不胜感激。
试试这个,
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
UIImage *image = [UIImage imageNamed: @"yourImageName.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imgView;
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)])
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],
UITextAttributeFont,
nil]];
}
将以下代码放入您的Appdelegate.m文件中
您可以使用以下代码:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Image.png"] forBarMetrics:UIBarMetricsDefault];
我是一个理想的人,因为我遇到了这样的问题,所以你可以尝试我的方法,好吗!
首先,创建一个导航控制器并将其显示在窗口上:
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:YourView];
接下来,创建一个带有图像属性的 Uiimageview 对象,并使用您想要添加到导航栏的图像和框架属性作为位置:
UIImageView *imgView=[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImagename"];
imgView.Frame=CGrectmake();
最后一步:将 imgView 对象作为子视图添加到导航栏中。
[nav.navigationBar addSubview:imgView];
完成了!!!!!!!!!!