简体   繁体   English

无法将背景图片设置为UINavigationController

[英]Can't set a background image to UINavigationController

I've added an image to my project. 我已经在项目中添加了图片。 Strangely, on the filesystem it is located in the root directory of the application, while in the project explorer within XCode I've put it in /Images/navbar-bg.png . 奇怪的是,在文件系统上,它位于应用程序的根目录中,而在XCode的项目浏览器中,我将其放在/Images/navbar-bg.png How to make this consistent? 如何使之一致?

By the way, I want o add it as background image to my UINavigationController. 顺便说一句,我想把它作为背景图像添加到我的UINavigationController中。 This is the code, but I see no changes from the default blue background: 这是代码,但是我看不到默认的蓝色背景有任何变化:

// navigation
navigationController = [[UINavigationController alloc] initWithRootViewController:cardListViewController];
// ps hue: 37, 53, -5
navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"navbar-bg" ofType:@"png"]]];

self.window.rootViewController = navigationController;

I've also tried UIImage imageNamed with no results. 我也尝试了UIImage imageNamed ,但没有结果。 I want to do it programmatically and I'm not using any xib here. 我想以编程方式执行此操作,并且在这里不使用任何xib。

Thanks 谢谢

Those yellow-looking folder icons you see in XCode's project explorer don't actually represent folders; 您在XCode的项目浏览器中看到的那些看起来黄色的文件夹图标实际上并不表示文件夹;它们只是代表文件夹。 they are groups . 他们是groups You can, of course, move the file out of the Images subfolder to make it consistent, but I presume you want to do the reverse. 当然,您可以将文件移出Images子文件夹,以使其保持一致,但我想您要相反。

To add items to your project and have them retain their source folder name, you have to create 'folder references'. 要将项目添加到项目中并使它们保留其源文件夹名称,则必须创建“文件夹引用”。 In the Add Files... dialog, choose the "Create folder references for any added folders" radio button. 在“添加文件...”对话框中,选择“为任何已添加的文件夹创建文件夹引用”单选按钮。 You'll get a blue folder icon instead of a yellow one; 您将获得一个蓝色的文件夹图标,而不是一个黄色的文件夹图标; all items in that folder will get added to your project, and any time you change the contents of that folder, the built executable will match its contents. 该文件夹中的所有项目都将添加到您的项目中,并且每当您更改该文件夹的内容时,生成的可执行文件都会与其内容匹配。

... and, you asked a second question ... ...,然后你问了第二个问题...

In iOS 5, to add an image to a navigation bar, use UINavigationBar:setBackgroundImage . 在iOS 5中,要将图像添加到导航栏中,请使用UINavigationBar:setBackgroundImage

Try this, Write this code in your viewDidload method. 试试这个,在您的viewDidload方法中编写此代码。 Thats it 而已

UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: @"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];

Thanks. 谢谢。

If it is the background colour of the navigation controller, then any controller being displayed in the navigation controller will be displayed on top of it. 如果它是导航控制器的背景色,那么导航控制器中显示的任何控制器都将显示在其顶部。 This means it will never be visible. 这意味着它将永远不可见。 What you are seeing is the cardlistcontroller, you will have to set the background image on this controller instead. 您所看到的是cardlistcontroller,您将不得不在此控制器上设置背景图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM