2

我花了几天的时间试图解决这个问题,因此我决定与您分享解决方案,以便您可以将更多时间用于您的家人:

假设您有一个 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

但这是一个合理的代价。

4

0 回答 0