• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 在点击点放置一个 UIView 以启动弹出窗口

[复制链接]
菜鸟教程小白 发表于 2022-12-11 17:02:03 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我认为解决从 UITableViewRowAction 启动弹出框问题的一种相当聪明的方法中,我想在目标操作上放置一个清晰的 UIView并将其用于 sourceRect 以锚定 UIPopoverPresentationController。在这种情况下,目标操作是下面屏幕截图中显示的橙色“Acct”按钮。

我在整个 tableview 上使用了一个清晰的 UIView (self.clearTopView) 并用一个框架(我认为)来自 View 中的点击位置(point)和一个CGSize(tappedRectSize)。然后我将它作为 subview 添加到 self.clearTopView。我现在把它涂成黑色,这样我就可以找到它了。

如下图所示,部分方案正在按计划进行。 tappedViewOverlay View 被添加,并适当着色。 popoverPresentationController (thisPPC) 按预期从该 View 启动。

enter image description here

问题是 View 始终位于屏幕的左上角,而不是所需的位置,即点击发生的位置。以下是相关代码:

    else if ([[segue identifier] isEqualToString"AccountPopSegue"])
    {

        UITapGestureRecognizer *tapGestureRecognizer;

        CGPoint point = [tapGestureRecognizer locationInView:self.clearTopView];

        NSString *pointString = NSStringFromCGPoint(point);

        NSLog(@"point is located at %@",pointString);


        CGSize tappedRectSize= CGSizeMake(60,60);
        CGRect tappedRect = {point,tappedRectSize};

        NSString * tappedRectString = NSStringFromCGRect(tappedRect);

        NSLog(@"tappedRect = %@",tappedRectString);


        tappedViewOverlay = [[UIView alloc]initWithFrame:tappedRect];
        tappedViewOverlay.backgroundColor = [UIColor blackColor];

        [self.clearTopView addSubview:tappedViewOverlay];

        [self.clearTopView setUserInteractionEnabled:NO];


        NSString *tappedOverlayFrame = NSStringFromCGRect(tappedViewOverlay.frame);
        NSLog(@"tappedViewOverlay.frame = %@",tappedOverlayFrame);


        UIViewController *controller = segue.destinationViewController;
        controller.popoverPresentationController.delegate = self;
        controller.preferredContentSize = CGSizeMake(320, 186);
        UIPopoverPresentationController *thisPPC = controller.popoverPresentationController;


        thisNavController = (UINavigationController *)segue.destinationViewController;
        AccountChangeVC *acCVC = (AccountChangeVC *)thisNavController.topViewController;
        acCVC.delegate = self;

        thisPPC.sourceView = self.clearTopView;
        thisPPC.sourceRect = tappedViewOverlay.bounds;

谁能告诉我哪里出错了?

编辑

CoderWang 指出了我的代码中的一个疏忽,所以我已经纠正了它:

    else if ([[segue identifier] isEqualToString"AccountPopSegue"])
    {

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]init];

        [self.clearTopView addGestureRecognizer:tapGestureRecognizer];



        CGPoint point = [tapGestureRecognizer locationInView:self.clearTopView];

        NSString *pointString = NSStringFromCGPoint(point);

        NSLog(@"point is located at %@",pointString);


        CGSize tappedRectSize= CGSizeMake(60,60);

        CGRect tappedRect = {point,tappedRectSize};

        NSString * tappedRectString = NSStringFromCGRect(tappedRect);

        NSLog(@"tappedRect = %@",tappedRectString);


        tappedViewOverlay = [[UIView alloc]initWithFrame:tappedRect];

        tappedViewOverlay.backgroundColor = [UIColor blackColor];

        [self.clearTopView addSubview:tappedViewOverlay];

        [self.clearTopView setUserInteractionEnabled:NO];


        NSString *tappedOverlayFrame = NSStringFromCGRect(tappedViewOverlay.frame);
        NSLog(@"tappedViewOverlay.frame = %@",tappedOverlayFrame);


        UIViewController *controller = segue.destinationViewController;
        controller.popoverPresentationController.delegate = self;
        controller.preferredContentSize = CGSizeMake(320, 186);
        UIPopoverPresentationController *thisPPC = controller.popoverPresentationController;


        thisNavController = (UINavigationController *)segue.destinationViewController;
        AccountChangeVC *acCVC = (AccountChangeVC *)thisNavController.topViewController;
        acCVC.delegate = self;

        thisPPC.sourceView = self.clearTopView;
        thisPPC.sourceRect = tappedViewOverlay.bounds;

此更改(tapGestureRecognizer 的初始化和对 self.clearTopView 的添加)在黑色 tappedViewOverlay View 的位置上产生了轻微的变化.您可以在以下屏幕截图中看到它隐藏在“返回”导航按钮后面:

enter image description here



Best Answer-推荐答案


问题是“tappedRect”不正确,不知 Prop 体原因。 但是您可以尝试以下方法将“Acct”按钮转换为 self.clearTopView 的矩形:

CGRect tappedRect = [Acct.superview convertRect:acct.frame toView:self.clearTopView];

或转换为窗口(因为 self.clearTopView 在整个 tableview 上):

CGRect tappedRect = [Acct.superview convertRect:acct.frame toView:nil];

关于ios - 在点击点放置一个 UIView 以启动弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38490169/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap