如何在iPhone sdk中向UINavigationBar添加自定义图像按钮 [英] How to add a customized image button to UINavigationBar in iPhone sdk

查看:80
本文介绍了如何在iPhone sdk中向UINavigationBar添加自定义图像按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UINavigation Bar中添加自定义箭头按钮,它位于顶部。

I want to add customized arrow buttons to UINavigation Bar which comes at the top.

我想要做的是创建一个自定义图像按钮并将其放在栏上。当我尝试添加到以下视图时:

what I want to do is create a custom image button and place it over the bar. When I tried adding to view like:

[self.view addSubview:leftArrowButton];//(Where leftArrowButton is a UIButton with an image of arrow)

这不起作用,就像放置按钮但按钮隐藏在栏下面并且不重叠。

This dont work, like it places the button but button gets hided under the bar and not overlap on it.

任何帮助表示赞赏。谢谢。

Any help is appreciated. Thanks.

推荐答案

尝试执行以下操作:

UINavigationBar *bar = self.navigationController.navigationBar;
bar.titleView = leftArrowButton;  // change title view
bar.leftBarButtonItem = leftArrowButton; // if ref is UIBarButtonItem

您应该考虑使用 UIBarButtonItem 而不是 UIButton

You should consider using a UIBarButtonItem rather than UIButton.

您还可以自定义条形按钮和图像(使用 initWithImage:style:target:action: ):

You can also customize the bar buttons as well with images (using the initWithImage:style:target:action:):

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:myImage
                             style:UIBarButtonItemStyleBordered
                            target:self action:@selector(buttonPressed:)];
bar.leftBarButtonItem = item;
[item release];

您还可以使用 initWithCustomView:

You can also use any view you like (with some consideration for the size) using the initWithCustomView::

UIBarButtonItem *item = [[UIBarButtonItem alloc]
                           initWithCustomView:leftArrowButton];
bar.leftBarButtonItem = item;
[item release];

这篇关于如何在iPhone sdk中向UINavigationBar添加自定义图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆