美文网首页
查找一个VIew在父视图中的位置

查找一个VIew在父视图中的位置

作者: 跬步千里_LenSky | 来源:发表于2018-07-17 18:32 被阅读120次
    +(CGRect)rectFromSunView:(UIView *)view {
        //查找frame
        UIView *vcView = [self rootViewFromSubView:view];
        UIView *superView = view.superview;
        CGRect viewRect = view.frame;
        CGRect viewRectFromWindow = [superView convertRect:viewRect toView:vcView];
        return viewRectFromWindow;
    }
    + (UIView *)rootViewFromSubView:(UIView *)view {
        UIViewController *vc = nil;
        UIResponder *next = view.nextResponder;
        do {
            if ([next isKindOfClass:[UINavigationController class]]) {
                vc = (UIViewController *)next;
                break ;
            }
            next = next.nextResponder;
        } while (next != nil);
        if (vc == nil) {
            next = view.nextResponder;
            do {
                if ([next isKindOfClass:[UIViewController class]] || [next isKindOfClass:[UITableViewController class]]) {
                    vc = (UIViewController *)next;
                    break ;
                }
                next = next.nextResponder;
            } while (next != nil);
        }
        return vc.view;
    }
    
    [YBPopupMenu] 地址 比较不错的弹出空间高度封装
    https://github.com/lyb5834/YBPopupMenu
    

    查找一个View 所在的控制器

    -(UIViewController *)findViewController:(UIView*)view {
        id responder = view;
        while (responder){
            if ([responder isKindOfClass:[UIViewController class]]){
                return responder;
            }
            responder = [responder nextResponder];
        }
        return nil;
    }
    

    相关文章

      网友评论

          本文标题:查找一个VIew在父视图中的位置

          本文链接:https://www.haomeiwen.com/subject/hdmgpftx.html