【发布时间】:2011-04-12 23:12:19
【问题描述】:
我在详细视图中创建了一个带有 UIToolbar 的拆分视图。我添加了一个 UILabel 以放置标题文本。我使用了一些建议来构建它,但我注意到在纵向模式下(当主视图的弹出按钮存在时),文本不太居中。它被弹出按钮的宽度所抵消。我试过减去弹出框的宽度,但灵活的垫片似乎把它放回去了。我还尝试了 self.titleLabel 的各种宽度(例如 self.view.frame.size.width)。在横向模式下居中工作正常(因为没有弹出按钮)。有人以前看过这个并有建议吗?谢谢!
- (void)toolbarTitleWithNSString:(NSString *)titleString {
NSMutableArray *items = [[self.toolbar items] mutableCopy];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[items addObject:spacer];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,
11.0f,
100.0f,
21.0f)];
[self.titleLabel setFont:[UIFont boldSystemFontOfSize:18.0]];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setShadowColor:UIColorFromRGB(0xe5e7eb80)];
[self.titleLabel setShadowOffset:CGSizeMake(0, -1.0)];
[self.titleLabel setTextColor:UIColorFromRGB(0x717880ff)];
[self.titleLabel setText:titleString];
[self.titleLabel setTextAlignment:UITextAlignmentCenter];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:self.titleLabel];
[items addObject:title];
[title release];
[items addObject:spacer];
[spacer release];
[self.toolbar setItems:items animated:YES];
[items release];
}
#pragma mark -
#pragma mark Managing the popover
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Add the popover button to the toolbar array.
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray insertObject:barButtonItem atIndex:0];
[toolbar setItems:itemsArray animated:NO];
[itemsArray release];
}
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Remove the popover button from the toolbar array.
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray removeObject:barButtonItem];
[toolbar setItems:itemsArray animated:NO];
[itemsArray release];
}
【问题讨论】:
-
所以我找到了一个解决方案,但我不知道它是最好的。我在 [self.toolbar setItems:items animated:YES] UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] 之前添加了以下代码固定空间.宽度=33.0f; [项目添加对象:固定空间]; [固定空间释放];
-
这在两个方向上都可以继续工作,因此看起来柔性垫片按预期处理了固定空间。 (fixedSpace 的宽度是 UIBarButtonItem 的宽度,但是,当我尝试读取宽度时,它总是返回 0,因此是硬编码;我稍后会处理。
-
UIBarButtonItem 的宽度,使用 barButtonItem.image.size.width。 (使用自定义图像)
-
返回值 20.f。我认为有 6 个像素的填充,所以将 fixedSpacer 更改为 32.0f。
标签: ipad uitableview uilabel uisplitviewcontroller