我花了几天时间试图解决这个问题,因此我决定与你分享解决方案,以便你能为你的家人奉献更多的时间:
假设您有一个UIToolbar,它需要容纳大量的按钮(比如5个)。
self.bookmarkButton = [[[UIBarButtonItem alloc] ...;
self.sendButton = [[[UIBarButtonItem alloc] ...;
self.backstageButton = [[[UIBarButtonItem alloc] ...;
self.voteButton = [[[UIBarButtonItem alloc] ...;
self.vipButton = [[[UIBarButtonItem alloc] ...;
UIBarButtonItem *sep0 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *sep1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *sep2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *sep3 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
[self setItems:[[[NSArray alloc] initWithObjects: self.bookmarkButton, sep0, self.sendButton, sep1, self.voteButton , sep2, self.backstageButton, sep3, self.vipButton, nil] autorelease]];
我尝试了一些技术,例如设置显式按钮宽度和删除分隔符,增加最左边和最右边按钮的宽度,但由于各种原因,这些技术都不起作用。
解决此问题最可靠的方法是再引入两个分隔符,一个位于最左侧按钮的左侧,另一个位于最右侧按钮的右侧。像这样的:
self.bookmarkButton = [[[UIBarButtonItem alloc] ...;
self.sendButton = [[[UIBarButtonItem alloc] ...;
self.backstageButton = [[[UIBarButtonItem alloc] ...;
self.voteButton = [[[UIBarButtonItem alloc] ...;
self.vipButton = [[[UIBarButtonItem alloc] ...;
UIBarButtonItem *sep0 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *sep1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *sep2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *sep3 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *sepLeft = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *sepRight = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
[self setItems:[[[NSArray alloc] initWithObjects: sepLeft, self.bookmarkButton, sep0, self.sendButton, sep1, self.voteButton , sep2, self.backstageButton, sep3, self.vipButton, sepRight, nil] autorelease]];
这完全解决了问题。当然,所有的按钮都稍微向中间移动,如图所示:
http://pics.livejournal.com/andre_po/pic/00004t7f
但这是一个合理的价格。