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

2018-07-17  本文已影响120人  跬步千里_LenSky
+(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;
}
上一篇下一篇

猜你喜欢

热点阅读