gpt4 book ai didi

ios - UITableViewRowAction 在 iOS 11 中太宽

转载 作者:可可西里 更新时间:2023-11-01 04:55:22 25 4
gpt4 key购买 nike

为了在 TableView 行操作中显示图像,我使用了以下技巧:

How to manage equal width for UITableViewRowAction in iOS8.0?(More,Delete,etc actions)

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

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

Screenshot

有解决办法吗?

更新:

我最终使用了 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.
}

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

最佳答案

我遇到了同样的问题,并且能够解决它。但是,我的解决方案很老套,需要一些解释:

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

我们希望避免我们的 subview 变得比我们的操作按钮的宽度总和更宽。在这种情况下,我们需要修复我们的 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++;
}
}
}
}
}
}

关于ios - UITableViewRowAction 在 iOS 11 中太宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46211091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com