苹果文档有这样的说法:
UIBarButtonSystemItemFlexibleSpace 要在其他项目之间添加的空白空间。空间在其他项目之间平均分配。设置此值时,将忽略其他项目属性。
说的有点模糊(究竟什么空间是平均分配的?)所以我写了一个测试方法:
-(void) createToolbar {
BOOL stuffInTopLeftCorner = NO;
UIToolbar* bar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 1024, 44)];
self.bar = bar;
[self addSubview:bar];
UILabel* titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
titleLabel.text = @"Centered title";
titleLabel.font = [UIFont systemFontOfSize:30];
[titleLabel sizeToFit];
CGSize titleSize = [titleLabel bounds].size;
NSLog(@"titleSize is %g %g", titleSize.width, titleSize.height);
UIBarButtonItem* titleItem = [[UIBarButtonItem alloc]initWithCustomView:titleLabel];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UILabel* leftLabel = [[UILabel alloc]initWithFrame:CGRectZero];
leftLabel.text = @"Stuff in top left corner";
leftLabel.font = [UIFont systemFontOfSize:30];
[leftLabel sizeToFit];
UIBarButtonItem* topLeftCornerItem = [[UIBarButtonItem alloc]initWithCustomView:leftLabel];
NSMutableArray* items = [NSMutableArray arrayWithObjects: flexibleSpace, titleItem, flexibleSpace, nil];
if (stuffInTopLeftCorner) {
[items insertObject:topLeftCornerItem atIndex:0];
}
bar.items = items;
}
这是上面代码的样子:
如果我将 stuffInTopLeftCorner 更改为 YES,这就是它的样子:
似乎在标题左侧添加某些内容根本不会导致所述标题移动。
我的问题是——这是否意味着它总是以标题为中心,而不管它的两边是什么?