我花了几天时间试图解决这个问题,因此我决定与您分享解决方案,以便您可以将更多时间投入到您的家庭中:
假设您有一个需要容纳大量按钮的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
但这是一个合理的代价。