【问题标题】:UILabel on a UIToolbar (programmatically) doesn't display the labelUIToolbar 上的 UILabel(以编程方式)不显示标签
【发布时间】:2013-04-08 23:26:45
【问题描述】:

我在 -viewDidLoad 中有以下代码::

self.navigationController.toolbarHidden = NO;

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]    initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(sendEmail:)];

    UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 25, self.view.frame.size.width, 21.0f)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"text-here";
    label.userInteractionEnabled = NO;
    label.font = [UIFont systemFontOfSize:18.0];
    label.textAlignment = NSTextAlignmentCenter;

    UIBarButtonItem *labelItem = [[UIBarButtonItem alloc] initWithCustomView:label];

    self.navigationController.toolbarItems = [NSArray arrayWithObjects:shareButton, spacer1, labelItem, nil];

但是,当我加载应用程序时,工具栏仅显示共享按钮,该按钮与通过电子邮件共享应用程序的内容相关联。标签没有出现(我希望它出现)。我基本上从 stackoverflow 上提出的其他问题中复制了这个确切的代码,而且我所看到的每个地方都是相同的答案......这有什么不同吗?

我假设由于共享按钮可以正常工作,因此工具栏也可以正常工作。像我这样的问题的其他答案产生了 [self.navigationController.view addSubview:toolbarObj];,但我只是使用了与导航控制器集成的工具栏。

垫片...嗯,我现在还不知道垫片对我有多大作用。现在,如果我理解正确,垫片会将我的标签项目推到最右边,对吗?它不会均匀地隔开物体吗?我希望将标签放在工具栏的中心。

非常感谢您的帮助和解释。

【问题讨论】:

    标签: objective-c text uinavigationcontroller uilabel uitoolbar


    【解决方案1】:

    当我运行您的代码时,它既不显示标签也不显示按钮。如果我在下面进行更改,它们都会出现:

    变化:

    self.navigationController.toolbarItems = [NSArray arrayWithObjects:shareButton, spacer1, labelItem, nil];
    

    到:

    self.toolbarItems = [NSArray arrayWithObjects:shareButton, spacer1, labelItem, nil];
    

    如果您希望标签位于中间,请将其缩小(我只是将视图宽度除以 2)并在其另一侧添加第二个间隔:

    self.toolbarItems = [NSArray arrayWithObjects:shareButton, spacer1, labelItem, spacer2, nil];
    

    【讨论】:

    • 有趣...它为我显示了分享按钮,并且工作正常。我现在无法访问该项目,但我想让你知道我知道你已经回答了,我很感激。我会让你知道它是否适合我(我相信它会)
    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    相关资源
    最近更新 更多