UITableViewRowAction在iOS 11中太宽

ios*_*ude 5 uitableview ios ios11

我使用以下技巧,以便在表视图行动作中包含图像:

如何在iOS8.0中管理UITableViewRowAction的相等宽度?(更多,删除等动作)

UITableViewRowAction *completeAction = [UITableViewRowAction
                                        rowActionWithStyle:UITableViewRowActionStyleNormal
                                        title:@"   "
                                        handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                                            ...
                                        }];
completeAction.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"MyImageHere"]];
Run Code Online (Sandbox Code Playgroud)

但它不再适用于iOS 11 - 行动作按钮的宽度对于我的图像来说太大了所以它会重复:

截图

有没有解决这个问题?

更新:

我最终使用了iOS 11中引入的新的尾随/前导上下文操作API:

https://developer.apple.com/documentation/uikit/uicontextualaction

此API允许您在操作中包含图像等.

#if defined __IPHONE_11_0 && __has_builtin(__builtin_available)

// This will be called on iOS 11+ if compiling with Xcode 9.

- (id)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (@available(iOS 11.0, *)) {
        UISwipeActionsConfiguration *configuration = ...
        return configuration;
    }
    return nil;
}

#endif

// This will be called on iOS 10 and older.

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView
                  editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Old style row actions.
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果使用Xcode 8编译这样的代码,则不会定义新的委托方法(您将看到该错误).

小智 0

我有同样的问题并且能够解决它。然而,我的解决方案非常老套,需要一些解释:

我们必须实现一个自己的UITableView ,它会在LayoutSubviews()中修改其子视图和子子视图的布局。包含操作按钮的视图是UISwipeActionPullView并且是子视图。这种类型在编译时不可用(也许有人知道如何检查这种类型?),但我们可以检查类型UIKit.UIView并检查视图是否具有与我们定义的操作按钮相同数量的子视图(UIButton) 。

我们希望避免子视图变得比操作按钮的宽度总和更宽。在这种情况下,我们需要修复UISwipeActionPullView的大小和位置。此外,我们需要修复UIButton的位置。

我的代码给出了三个固定宽度操作按钮的示例:

class CustomTableView: UITableView
{
    public CustomTableView(CGRect frame, UITableViewStyle style) : base(frame, style)
    {
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        var buttonSize = 54;

        foreach (var actionView in Subviews)
        {
            if (actionView.GetType().FullName == "UIKit.UIView" && actionView.Subviews.Length == 3)
            {
                if (actionView.Frame.Width >= buttonSize* 3)
                {
                    actionView.Frame = new CGRect(Frame.Width - 3 * buttonSize, actionView.Frame.Y, buttonSize* 3, buttonSize);
                    int i = 0;
                    foreach (var button in actionView.Subviews)
                    {
                        button.Frame = new CGRect(i * buttonSize, 0, buttonSize, buttonSize);
                        i++;
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)