UIActionSheet在iOS 7 GM上未显示最后一项的分隔符

46

可能是iOS7上的一个错误。但是最后一个按钮与前一个按钮没有分开。UIActionSheet is missing separator on the last button

正如您从图像中所看到的那样。在使用iOS7 GM的模拟器和设备上都会出现此问题。 其他人是否也遇到了同样的问题?

UIActionSheet *actionSheet = [[UIActionSheet alloc]
               initWithTitle:@"Title"
               delegate:self
               cancelButtonTitle:nil
               destructiveButtonTitle:nil
               otherButtonTitles:@"First", @"Second", @"Third", @"Fourth", nil];
[actionSheet showInView:self.view];

你可以看到,这段代码非常简单。 有什么想法可以解决这个问题吗?或者有哪些第三方库可以代替UIActionSheet?


2
很可能这是iOS的一个bug。提交bug报告。我在UITableViews中看到了类似的情况。一些行缺少分隔符。我认为不值得尝试使用第三方库来修复这个问题。我相信很快就会有GM2版本发布。 - Matthias Bauch
8
这篇文章建议在iPad上将@""传递给cancelButtonTitle作为一种解决方法。 - wattson12
解决方法奏效了! - nacho4d
1
@wattson12 这是一个不错的解决方法,但如果我不想要取消按钮,我该怎么办? - JJ86
8个回答

24

我认为ActionSheet需要一个取消按钮,这样你就可以添加取消按钮标题。

另一种方法是:指定actionSheet的cancelButtonIndex。

例如,在您的情况下,您可以在otherButtonTitles中添加一个“取消”,位置在索引4处,然后指定actionSheet.cancelButtonIndex = 4。


4
起初我认为这不是理想情况,但我没有意识到,如果你添加一个取消按钮,iPad 足够聪明,不会显示它。(在 iOS 6 中也是如此。)由于 iPhone 应该有一个取消按钮,所以这个解决方案非常完美。 - robotspacer
1
我认为ActionSheet需要一个取消按钮,但事实并非如此。苹果在文档中指出:“如果您不想要取消按钮或正在iPad上呈现操作表,则指定nil。”然而,像下面发布的那样,用@""代替nil对我有效。 - Stephen Watson

8

我找到了一种最简单的方法,在iPhone和iPad上使其工作:

  1. 只用标题初始化UIActionSheet
  2. 添加你的按钮
  3. 在最后添加一个“CANCEL”按钮
  4. 将CancelButtonIndex设置为最后一个索引

我认为缺失的分隔符是由于在第一次添加或通过init时未能将取消按钮识别为单独的情况。


这是正确的方式。 - Legoless
同意,在为这个问题(UIActionSheet 在iPhone/iPad 和iOS7/iOS8上保持一致)浪费了一天后,我最终采用了这种方法。此外,我将在第1步和第2步之间添加破坏性按钮(即作为第一个按钮)。 - Sebastian Greifeneder

6
我发现,在初始化后添加一个空字符串的取消按钮可以解决问题。取消按钮不会显示出来,而分隔符会显示出来。
[sheet addButtonWithTitle: @""];
[sheet setCancelButtonIndex: sheet.numberOfButtons - 1];

但是这仅适用于iPad。在iPhone上,将显示一个空的取消按钮,但我找到了一个hacky解决方法使其正常工作。除了上述内容外,在willPresentActionSheet中添加以下代码:
NSInteger offset = 55;
CGRect superFrame = actionSheet.superview.frame;
superFrame.origin.y += offset;
[actionSheet.superview setFrame: superFrame];

// hide underlay that gets shifted with the superview
[(UIView*)[[actionSheet.superview subviews] objectAtIndex: 0] removeFromSuperview];

// create new underlay
CGRect underlayFrame = CGRectMake(0, -offset, superFrame.size.width, superFrame.size.height);
UIView* underlay = [[UIView alloc] initWithFrame: underlayFrame];
underlay.alpha = 0.0f;
[underlay setBackgroundColor: [UIColor colorWithWhite: 0.0f alpha: 0.4f]];
[actionSheet.superview insertSubview: underlay atIndex: 0];

// simulate fade in
[UIView animateWithDuration: 0.3f animations:^{
    underlay.alpha = 1.0f;
}];

这会使整个表格下移,以使得取消按钮隐藏在屏幕之外。

5
最简单的解决方法是在分配时将nil替换为@""以用作取消按钮标题。
UIActionSheet *actionSheet = [[UIActionSheet alloc]
               initWithTitle:@"Title"
               delegate:self
               cancelButtonTitle:@"" // change is here
               destructiveButtonTitle:nil
               otherButtonTitles:@"First", @"Second", @"Third", @"Fourth", nil];
[actionSheet showInView:self.view];

3
UIActionSheet *asAccounts = [[UIActionSheet alloc]
                            initWithTitle:Localized(@"select_an_account")
                            delegate:self
                            cancelButtonTitle:nil
                            destructiveButtonTitle:nil
                            otherButtonTitles: nil];

for (int i=0; i<[result count]; i++) {
    ACAccount *acct = [result objectAtIndex:i];
    [asAccounts addButtonWithTitle:[acct username]];
    asAccounts.tag = i;
}

[asAccounts addButtonWithTitle:Localized(@"Cancel")];                
asAccounts.cancelButtonIndex = result.count;
[asAccounts showInView:self.view];

1
如果您有一个取消按钮,最后一行将会显示。这是我现在使用的临时解决方案。如果您不想显示取消按钮,则不知道任何解决方案。

0
    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    if ([UIDevice currentDevice].systemVersion.floatValue < 8.0f) {
        UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(8, 88, actionSheet.frame.size.width - 16, 0.5)];
        separator.backgroundColor = [UIColor colorWithRed:219.0f/255 green:219.0f/255 blue:223.0f/255 alpha:1];
        [actionSheet addSubview:separator];
    }
}

每个按钮的高度都是44。我的ActionSheet没有标题。我想在第二个和第三个按钮之间添加分隔线。这就是为什么我使用数字88的原因。

0

看起来,initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles: 方法有 bug,你不应该在这里指定任何取消按钮。此外,在该方法中设置破坏性按钮似乎也不太好用。

相反,你应该:

  • 将自定义的取消按钮作为按钮数组中的最后一个按钮提供,例如:["操作1", "操作2", "关闭"]sheet.addButtonWithTitle("关闭")
  • 手动设置取消按钮的索引,例如:sheet.cancelButtonIndex = 2

然后一切都会像预期的那样工作。在 iPad 上,按钮将被自动隐藏,在 iPhone 上,按钮将被设置并放置在适当的位置上。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接