将 UILabel 添加到 UIToolbar
我正在尝试向我的工具栏添加标签。 按钮工作得很好,但是当我添加标签对象时,它崩溃了。 有任何想法吗?
UIBarButtonItem *setDateRangeButton = [[UIBarButtonItem alloc] initWithTitle:@"Set date range"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(setDateRangeClicked:)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
label.text = @"test";
[toolbar setItems:[NSArray arrayWithObjects:setDateRangeButton,label, nil]];
// Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
// Reload the table view
[self.tableView reloadData];
I'm trying to add a label to my toolbar. Button works great, however when I add the label object, it crashes. Any ideas?
UIBarButtonItem *setDateRangeButton = [[UIBarButtonItem alloc] initWithTitle:@"Set date range"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(setDateRangeClicked:)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
label.text = @"test";
[toolbar setItems:[NSArray arrayWithObjects:setDateRangeButton,label, nil]];
// Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
// Reload the table view
[self.tableView reloadData];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我使用这个技巧的目的之一是在
UIToolBar
之上实例化一个UIActivityIndicatorView
,否则这是不可能的。 例如,这里我有一个带有 2 个 UIBarButtonItem 的 UIToolBar、一个 FlexibleSpaceBarButtonItem 和另一个 UIBarButtonItem。 我想在灵活空间和最终(右侧)按钮之间的UIToolBar
中插入UIActivityIndicatorView
。 因此,在我的RootViewController
中,我执行以下操作,One of the things I'm using this trick for is to instantiate a
UIActivityIndicatorView
on top of theUIToolBar
, something that otherwise wouldn't be possible. For instance here I have aUIToolBar
with 2UIBarButtonItem
, aFlexibleSpaceBarButtonItem
, and then anotherUIBarButtonItem
. I want to insert aUIActivityIndicatorView
into theUIToolBar
between the flexible space and the final (right-hand) button. So in myRootViewController
I do the following,详细信息
完整示例
结果
Details
Full sample
Result
与 Matt RI 使用的界面生成器类似。 但我想在里面有 1 个
UIWebView
,这样我就可以将一些文本加粗,而其他文本则不加粗(如邮件应用程序)。 所以IBOutlet
连接所有内容html
获得透明背景,以便工具栏通过代码发光:
Similar to Matt R I used interface builder. But I wanted to have 1
UIWebView
inside instead so that i can have some text bolded and other text not (like the mail app). SoIBOutlet
html
to have a transparent background so the toolbar shines throughCode:
如果您想在工具栏视图上添加视图,您可以尝试以下操作:
If you want to adding a view up the toolbar view you can try this:
试试这个:
注意:
self.barItem
是一个从对象库添加的UIBarButtonItem
,并放置在两个灵活空间之间。另一种方法是删除
[self.barItem setCustom:view]
行并更改label
的参数(宽度),使其填充整个工具栏并设置对齐方式自己在代码中设置中间和字体,Try this:
Note:
self.barItem
is aUIBarButtonItem
added from the object library and placed between two flexible spaces.another way is to remove the
[self.barItem setCustom:view]
line and change the parameters of thelabel
(width) so that it fills the entire toolbar and set the alignment to middle and the font by yourself in code,可以使用禁用的 BarButtonItem
could use a disabled BarButtonItem
Swift 中的解决方案
实现此目的的一种方法是创建一个
UILabel
,然后将其作为自定义视图添加到您可以使用的UIBarButtonItem
然后添加到工具栏。例如:
下面是其外观的屏幕截图:
A solution to this in Swift
One way you could do this is by creating a
UILabel
and then adding it as custom view to aUIBarButtonItem
which you then add to the toolbar.For example:
Here's a screenshot of what this would look like:
看一下
本质上每个项目都必须是一个“按钮”,但它们可以使用您需要的任何视图进行实例化。 这是一些示例代码。 请注意,由于其他按钮通常位于工具栏上,因此在标题按钮的两侧放置了间隔符,使其保持居中。
Have a look into this
Essentially every item must be a "button" but they can be instantiated with any view you require. Here is some example code. Note, since other buttons are typically on the toolbar, spacers are placed on each side of the title button so it stays centered.
对于那些使用 Interface Builder 来布局
UIToolBar
的人,也可以单独使用 Interface Builder 来完成此操作。要将
UILabel
添加到UIToolBar
,您需要通过拖动将通用UIView
对象添加到 IB 中的UIToolBar
UIToolBar
上的新UIView
对象。IB
将自动创建一个UIBarButtonItem
,并将使用您的自定义UIView
进行初始化。 接下来将UILabel
添加到UIView
并以图形方式编辑UILabel
以匹配您喜欢的样式。 然后,您可以根据需要直观地设置固定和/或可变垫片,以适当定位您的UILabel
。您还必须将
UILabel
和UIView
的背景设置为clearColor
以使UIToolBar
显示出来正确位于UILabel
下。For those using Interface Builder to layout your
UIToolBar
, it is also possible to do this using Interface Builder alone.To add a
UILabel
to aUIToolBar
you need to add a genericUIView
object to yourUIToolBar
in IB by dragging a newUIView
object over yourUIToolBar
.IB
will automatically create aUIBarButtonItem
that will be initialized with your customUIView
. Next add aUILabel
to theUIView
and edit theUILabel
graphically to match your preferred style. You can then visually set up your fixed and/or variable spacers as desired to position yourUILabel
appropriately.You must also set the background of both the
UILabel
and theUIView
toclearColor
to get theUIToolBar
to show through correctly under theUILabel
.我发现answerBot的答案非常有用,但我认为我在界面生成器中找到了一种更简单的方法:
Builder
将此 BarButtonItem 插入类中的属性(位于
Swift,但在 Obj-C 中非常相似):
为标签本身添加另一个属性:
在 viewDidLoad 中,添加以下代码来设置 Label 的属性
标签,并将其添加为 BarButtonItem 的 customView
要更新
UILabel
文本:结果:
每次更新标签文本时都必须调用
lastUpdateLabel.sizetoFit()
I found answerBot's answer very useful, but I think I found an even easier way, in Interface Builder:
Builder
plug this BarButtonItem to a property in your class (this is in
Swift, but would be very similar in Obj-C):
add another property for the Label itself:
in viewDidLoad, add the following code to set the properties of your
label, and add it as the customView of your BarButtonItem
To update the
UILabel
text:Result :
You have to call
lastUpdateLabel.sizetoFit()
each time you update the label text