【问题标题】:UIActionSheet is not showing separator on the last item on iOS 7 GMUIActionSheet 没有在 iOS 7 GM 的最后一项上显示分隔符
【发布时间】:2013-09-18 09:50:35
【问题描述】:

这可能是 iOS7 上的一个错误。但是最后一个按钮没有和上一个分开

从图片中可以看出。这发生在模拟器和使用 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 ?

【问题讨论】:

  • 这很可能是一个 iOS 错误。 File a bugreport。我在 UITableViews 中看到了类似的东西。某些行缺少分隔符。我认为尝试通过使用第三方库来解决这个问题是不值得的。我很确定很快就会有 GM2。
  • devforums.apple.com/message/857939#857939 这建议将 @"" 传递给 cancelButtonTitle (iPad)
  • 解决方法奏效了!
  • @wattson12 这是一个很好的解决方法,但如果我不想要取消按钮,我该怎么办?

标签: iphone ios uikit uiactionsheet ios7


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我认为 ActionSheet需要取消按钮。因此您可以添加取消按钮标题。

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

    例如,在您的情况下,您可以在索引 4 处的 otherButtonTitles 中添加“Cancel”,然后指定 actionSheet.cancelButtonIndex = 4.

    【讨论】:

    • 一开始我并不认为这很理想,但我没有意识到,如果你添加一个取消按钮,iPad 足够聪明,不会显示它。 (在 iOS 6 中也是如此。)因为 iPhone 应该有一个取消按钮,所以这个解决方案非常有效。
    • “我认为 ActionSheet 需要取消按钮...”并非如此。 Apple 在文档中声明:“......如果您不想要取消按钮或在 iPad 上显示操作表,请指定 nil。”但是,添加 @"" 而不是 nil 对我有用,如下所示。
    【解决方案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];
    

    【讨论】:

      【解决方案4】:

      我发现在初始化后添加一个带有空字符串的取消按钮是可行的。取消按钮不会出现,而分隔符会出现。

      [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];
        

        【讨论】:

          【解决方案6】:

          我找到了一种让它在 iPhone 和 iPad 上以最简单的方式运行的方法:

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

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

          【讨论】:

          • 这是正确的方法。
          • 同意,在这个问题上浪费了一天(UIActionSheet 与 iPhone/iPad 和 iOS7/iOS8 一致)后,我最终使用了这种方法。此外,我在第 1 步和第 2 步之间添加了破坏性按钮(即作为第一个按钮)。
          【解决方案7】:

          似乎 initWithTitle:delegate:cancelButtonTitle:破坏性ButtonTitle:otherButtonTitles: 方法有问题,您根本不应该在此处指定任何取消按钮。顺便说一句,在那个方法中设置的破坏性按钮似乎也不能很好地工作。

          你应该:

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

          然后一切都会按预期进行。在 iPad 上,该按钮将自动隐藏,而在 iPhone 上,它将以正确的方式设置样式和放置。

          【讨论】:

            【解决方案8】:
                - (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 号的原因。

            【讨论】:

              猜你喜欢
              • 2012-08-01
              • 1970-01-01
              • 1970-01-01
              • 2012-12-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-17
              • 2017-10-06
              相关资源
              最近更新 更多