首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滑动时如何自定义表视图中的UIContextualAction

滑动时如何自定义表视图中的UIContextualAction
EN

Stack Overflow用户
提问于 2018-10-29 15:06:17
回答 1查看 989关注 0票数 0

我需要创建一个带有图像和文本的动作按钮。下面的图片提供了一个例子。

https://i.stack.imgur.com/KEuHn.png

我已经创建了一个方法

代码语言:javascript
复制
public UIContextualAction ContextualFlagAction(int row)
    {
        var action = UIContextualAction.FromContextualActionStyle(UIContextualActionStyle.Normal, "Flag", Handler);
            (contextualAction, view, handler) =>
            {
                Console.WriteLine("Hello World!");
                handler(false);
            });

        action.Image = UIImage.FromFile(ResourceIdentifiers.DocumentIcon);

        return action;
    }

但这不是我需要做的。

如何将此操作自定义为上面的图像。

EN

回答 1

Stack Overflow用户

发布于 2018-10-30 03:36:28

可能您的问题是图像结果,您的代码已经设置了action.image,如果您有一个包含图片和标签的图像,图片是向上的,标签是向下的,会有您想要的。

代码语言:javascript
复制
public UIContextualAction ContextualFlagAction(int row)
{
    var action = UIContextualAction.FromContextualActionStyle(UIContextualActionStyle.Normal, "Flag", Handler);
        (contextualAction, view, handler) =>
        {
            Console.WriteLine("Hello World!");
            handler(false);
        });

    action.Image = UIImage.FromFile(ResourceIdentifiers.DocumentIcon);
    //this is your setted image
    return action;
}

更多信息:

您可以在TableViewCell中自定义Xamarin.ios。

在视图单元格中编写以下方法,在视图单元格中重写DidTransitionToState方法,可以用按钮替换操作

代码语言:javascript
复制
 private UITableView tableViewThis;

    public TableViewCellClass(UITableView tableView)
    {
        this.tableViewThis = tableView;
        
    }
 public override void DidTransitionToState(UITableViewCellState mask)
    {
        base.DidTransitionToState(mask);
        if ((mask & UITableViewCellState.ShowingDeleteConfirmationMask) == UITableViewCellState.ShowingDeleteConfirmationMask)
        {

            foreach (UIView subview in tableViewThis.Subviews)
            {
                if (subview.Class.Equals("UIContextualAction"))
                    //Delete the delete button of the system
                tableViewThis.WillRemoveSubview(subview);
                subview.BackgroundColor = UIColor.Clear;

                UIButton editBtn = new UIButton(UIButtonType.Custom);
                editBtn.Frame = new CGRect(10, 4, 50, 65);
                editBtn.SetBackgroundImage(UIImage.FromFile("1.png"), UIControlState.Normal);
                editBtn.AdjustsImageWhenHighlighted = false;
                editBtn.TouchUpInside += (sender, e) =>
                {
                    //do something you need
                };
                subview.AddSubview(editBtn);
            }
        }
    }

UIButton可以同时设置标题和图像。UIButton有两个属性:

titleEdgeInsets(top,左,下,右)

imageEdgeInsets(top,左,下,右)。

通过设置这两个,您可以实现所需的样式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53048355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档