是否为leftBarButtonItem忽略了UIBarButtonItem的customViews

时间:2012-01-27 16:51:21

标签: iphone uinavigationcontroller uinavigationitem

从文档中,我看到backBarButtonItem:

  

当此属性为nil时,导航项将使用其中的值   标题(页面10)属性,用于创建适当的后退按钮。如果你   想要为后退按钮指定自定义图像或标题,您可以   指定自定义栏按钮项(带有自定义标题或图像)   这个属性而不是。配置条形按钮项时,请不要这样做   为其分配自定义视图;导航项忽略自定义视图   无论如何,在后栏按钮。

我不知道leftBarButtonItem是否相同?基本上我有这个代码:

UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIImage *homeImage = [UIImage imageNamed:@"icon_house.png"];
[homeButton setImage:homeImage forState:UIControlStateNormal];
[homeButton addTarget:self action:@selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *homeBBI = [[UIBarButtonItem alloc] initWithCustomView:homeButton];

在iOS 5之前,我把它放在UIToolBar中,它显示得很好。现在我想把它作为leftBarButtonItem放在UINavigationController的backButton的右边。当我设置它时,它根本不显示。没有图像。但是,当我创建这样的按钮时:

    UIBarButtonItem *hButton = [[UIBarButtonItem alloc] initWithTitle:@"home" style:UIBarButtonItemStylePlain target:self action:@selector(homePressed:)];

并将其设置为leftBarButtonItem,它会显示出来。我不知道如何在没有边框的情况下获得我的主页按钮的自定义图标。当我使用时:

    UIBarButtonItem *hButton2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_house.png"] style:UIBarButtonItemStylePlain target:self action:@selector(homePressed:)];

我的house_icon周围有一个我不想要的边框。感谢。

2 个答案:

答案 0 :(得分:2)

试试这个

UIButton *TastoVersamento = [UIButton buttonWithType:UIButtonTypeCustom];
        [TastoVersamento setImage:[UIImage imageNamed:@"Versamento.png"] forState:UIControlStateNormal];
        [TastoVersamento addTarget:self action:@selector(Click_Versamento:) forControlEvents:UIControlEventTouchUpInside];
        [TastoVersamento setFrame:CGRectMake(0, 0, 40, 40)];
        [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:TastoVersamento] autorelease]];

答案 1 :(得分:1)

backBarButtonItemleftBarButtonItem不同。如果没有给出backBarButtonItem,则leftBarButtonItem是默认值。如果leftBarButtonItemleftItemsSupplementBackButton,即使设置了YES,它也会显示。

在第一个示例中,您可以尝试[homeButton sizeToFit]。我认为框架可能没有正确设置。

但至于你的上一次评论,我认为你不会成功地从导航项按钮中删除边框。我很确定它们是硬编码的。事实上,如果你添加一个带边框的按钮,你会得到两个,一个来自按钮,一个来自导航项。

相关问题