我试图将一些文本放在UIToolBar上,两边都有一个按钮,但是我遇到了一些问题。
我有以下代码:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:toolbarInitialFrame];
toolBar.barStyle = UIBarStyleBlackTranslucent;
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 25)];
[label setText:@"Title"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:16]];
UIBarButtonItem * labelEmu=[[UIBarButtonItem alloc] initWithCustomView:label];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAlarmAndDismissDatePicker:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(setAlarmAndDismissDatePicker:)];
所以我想要的是左边的取消按钮(cancelButton
),中间居中的标题(labelEmu
)和右边的完成按钮(doneButton
) 。像这样:
|[Cancel] Title [Done]|
我希望我能用这个达到目的:
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, doneButton, nil]];
但不幸的是,我得到的是:
|[Cancel]Title [Done]|
经过一番游戏后,我发现了这一行:
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, nil]];
...产生这个:
|[Cancel] Title |
标题居中的地方,忽略取消按钮在那里的事实。但是当我这样做时:
[toolBar setItems:[NSArray arrayWithObjects: spacer, labelEmu, spacer, doneButton, nil]];
......它产生了这个:
| Title [Done]|
标题位于完成按钮的最左边和右边之间的空间中。它不会忽略完成按钮,就像忽略取消按钮一样。
有人能指出我正确的方向来获得我追求的目标吗?抱歉,所有的naff ASCII图表! :)
答案 0 :(得分:2)
也许尝试使用:
[[UIBarButtonItem alloc] initWithTitle:@"yourtitle" style:UIBarButtonItemStylePlain target:nil action:nil];
如果UITollbar将在标签上负责,可能会有所帮助。
修改强> 尝试添加:
[label sizeToFit];
在将其添加到tollBar之前
答案 1 :(得分:1)
--- --- EDITED
经过进一步调查 - 事实证明问题确实是标签。您已将其宽度设置为200。
正确自动调整应该少得多。
请参阅此代码:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 200, 320, 50)];
toolBar.barStyle = UIBarStyleBlackTranslucent;
[self addSubview:toolBar];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 25)];
[label setText:@"Title"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[label sizeToFit];
UIBarButtonItem * labelEmu=[[UIBarButtonItem alloc] initWithCustomView:label];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAlarmAndDismissDatePicker:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(setAlarmAndDismissDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, doneButton, nil]];
它产生了这样的东西: