自定义以编程方式创建的导航栏的背景图像
我一直在自定义应用程序的导航栏。 我以编程方式添加了导航控制器。 事实上,在我的应用程序委托的方法 application:didFinishLanchingWithOptions 中, 我使用以下代码添加并隐藏了导航栏:
self.window.rootViewController = self.viewController;
// ListingNav previously declared as instance variable
self.ListingNav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.ListingNav.navigationBarHidden = YES;
[self.window addSubView:ListingNav.view];
[self.window makeKeyAndVisible];
return YES;
现在,我想将图像设置为导航栏背景,为“后退”按钮设置另一个背景图像,并在导航栏的右侧添加另一个按钮相同的导航栏也有自己的背景图像。关于如何进行的任何想法?
感谢您的帮助,
斯蒂芬
I'm stuck at customizing the navigation bar of my app.
I added the navigation controller programmatically.
Indeed, in the method application:didFinishLanchingWithOptions of my app delegate,
I've added and hidden a navigation bar using the following code:
self.window.rootViewController = self.viewController;
// ListingNav previously declared as instance variable
self.ListingNav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.ListingNav.navigationBarHidden = YES;
[self.window addSubView:ListingNav.view];
[self.window makeKeyAndVisible];
return YES;
Now, I'd like to set an image as a navigation bar background, set another background image for the "back" button and add another button on the right side of the same navigation bar with its own background image too. Any idea on how to proceed ?
Thx for helping,
Stephane
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"ImageNameWithOutExtension"] forBarMetrics: UIBarMetricsDefault];
You can try:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"ImageNameWithOutExtension"] forBarMetrics: UIBarMetricsDefault];
子类 UINavigationBar 并重写它的 drawRect 方法来绘制图像。
如果您的 UINavigationController 是通过 InterfaceBuilder 创建的,您可以轻松更改关联的 UINavigationBar 的类。如果您通过代码创建 UINavigationController,则可以使用
object_setClass
。 (这个例子)有很多关于StackOverflow 已经讨论了完全相同的主题,请毫不犹豫地在网站上搜索“NavigationController 背景”。
Subclass UINavigationBar and override its drawRect method to draw an image.
If your UINavigationController is then created thru InterfaceBuilder, you can easily change the class of the associated UINavigationBar. If you create your UINavigationController thru code, you can use
object_setClass
. (this one for example)There is a lot of questions on StackOverflow already on the exact same subject, don't hesitate to search for "NavigationController background" on the site.
您可以自定义导航栏而不隐藏它。只需使用 addSubview 方法添加这些组件即可。
添加背景图片的方法如下:
由于需要自定义按钮,因此可以用同样的方式添加UIButton。创建一个具有自己背景的新 UIButton 并添加它。
You can customize the navigation bar without hiding it. Just add these components using addSubview method.
The background image can be added as follows:
Since you need a custom button, you can add UIButton in the same way. Create a new UIButton with its own background and add it.
使用此代码......
Use This Code.....