自定义以编程方式创建的导航栏的背景图像

发布于 2024-12-03 18:53:37 字数 616 浏览 0 评论 0原文

我一直在自定义应用程序的导航栏。 我以编程方式添加了导航控制器。 事实上,在我的应用程序委托的方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

且行且努力 2024-12-10 18:53:37

您可以尝试:

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

You can try:

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

相思故 2024-12-10 18:53:37

子类 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.

清风无影 2024-12-10 18:53:37

您可以自定义导航栏而不隐藏它。只需使用 addSubview 方法添加这些组件即可。

添加背景图片的方法如下:

UIImageView *imageview = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"bg.png"]];
[self.navigationController.navigationBar addSubview:imageview];

[self.navigationController.navigationBar sendSubviewToBack:imageview];
[imageview release];

由于需要自定义按钮,因此可以用同样的方式添加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:

UIImageView *imageview = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"bg.png"]];
[self.navigationController.navigationBar addSubview:imageview];

[self.navigationController.navigationBar sendSubviewToBack:imageview];
[imageview release];

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.

古镇旧梦 2024-12-10 18:53:37

使用此代码......

     [self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@""]]];

Use This Code.....

     [self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@""]]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文