【问题标题】:are customViews for UIBarButtonItems ignored for the leftBarButtonItem为 leftBarButtonItem 忽略 UIBarButtonItems 的 customViews
【发布时间】:2012-02-20 15:52:54
【问题描述】:

从文档中,我看到了 backBarButtonItem:

当此属性为 nil 时,导航项使用其中的值 title (page 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 周围有一个我不想要的边框。谢谢。

【问题讨论】:

    标签: iphone uinavigationcontroller uinavigationitem


    【解决方案1】:

    backBarButtonItemleftBarButtonItem 是不同的。如果没有给出leftBarButtonItembackBarButtonItem 是默认值。如果leftItemsSupplementBackButtonYES,即使设置了leftBarButtonItem,它也会显示。

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

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

    【讨论】:

    • 粗鲁。你提前设置了框架......无论如何,我认为你的第二个例子就是你想要的:initWithImage。 (按钮内的按钮很奇怪。)initWithImage 是我所做的。正如我所说,我不相信你能移除边框。
    【解决方案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]];
    

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      相关资源
      最近更新 更多