简体   繁体   English

无法正确设置导航栏背景图片

[英]Can't set my navigation bar background image properly

I have a view, and inside this view I have a table view. 我有一个视图,在这个视图内有一个表格视图。

I want to change this table view navigation bar image to some image. 我想将此表视图导航栏图像更改为某些图像。

So I added a navigation bar object in the interface builder and put it on the table view > I control dragged from this object to the table view controller > then in the viewDidLoad I adder the line: [self.navBar setBackgroundImage:[UIImage imageNamed:@"nav.png"] forBarMetrics:UIBarMetricsDefault]; 因此,我在界面构建器中添加了一个导航栏对象,并将其放在表格视图中>我控制了从该对象拖到表格视图控制器中>在viewDidLoad中添加了以下行: [self.navBar setBackgroundImage:[UIImage imageNamed:@"nav.png"] forBarMetrics:UIBarMetricsDefault];

With navBar is the outlet to the navigation bar. 使用navBar是导航栏的出口。

Why this does not work? 为什么这不起作用?

tnx TNX

尝试在AppDelegate中添加以下代码

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav.png"] forBarMetrics:UIBarMetricsDefault];

Ideally, Navigation Bar should display the background image with the method you used until and unless the project resources contain the image and Navigation Bar is referenced properly. 理想情况下,导航栏应使用您之前使用的方法显示背景图像,除非项目资源中包含该图像且导航栏已正确引用。

Anyways, you can embed the View in Navigation Controller 1. Select ViewController in Storyboard 2. Go to Editor -> Embed in -> Navigation Controller and use the following code in viewDidAppear- 无论如何,您都可以在View Controller中嵌入视图。1.在Storyboard 2中选择ViewController。转至Editor-> Embed in-> Navigation Controller并在viewDidAppear-中使用以下代码。

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

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

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