我已经在详细视图中使用UIToolbar创建了分割视图。我添加了一个UILabel以放置标题文本。我用一些建议来构建它,但我注意到,当处于纵向模式时(当主视图的弹出按钮存在时),文本不是居中。它被弹出按钮的宽度所抵消。我尝试减去弹出窗口的宽度,但弹性隔离框似乎将其放回。我还尝试了各种宽度的self.titleLabel(例如self.view.frame.size.width)。居中在横向模式下工作正常(因为没有弹出式按钮)。任何人都看到这个,并有一个建议?谢谢!在分割视图的UIToolbar上正确对齐文本注释
- (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]; fixedSpace.width = 33.0f; [items addObject:fixedSpace]; [fixedSpace release]; – TimN 2011-04-13 19:01:50
这继续在两个方向上工作,所以看起来柔性垫片如预期的那样处理固定空间。 (固定空间的宽度是UIBarButtonItem的宽度,但是,当我尝试读取宽度时,它总是返回0,因此是硬编码;我将在稍后处理)。 – TimN 2011-04-13 19:05:00
通过使用barButtonItem.image.size.width,UIBarButtonItem的宽度。 (使用自定义图像) – TimN 2011-04-13 19:14:17