• 如果以一个树状的形式返回一个UIView的所有子视图


    该方法也是从一个视频中看到,总觉得会有很大作用,故记录在这里。

    它返回一个xml的字符串,用火狐浏览器或者其他可以格式化xml的工具打开,即可查看其层级关系。

    /**
     *  返回传入view的所有层级结构
     *
     *  @param view 需要获取层级结构的view
     *
     *  @return 字符串
     */
    - (NSString *)digView:(UIView *)view
    {
        if ([view isKindOfClass:[UITableViewCell class]]) return @"";
        // 1.初始化
        NSMutableString *xml = [NSMutableString string];
       
        // 2.标签开头
        [xml appendFormat:@"<%@ frame="%@"", view.class, NSStringFromCGRect(view.frame)];
        if (!CGPointEqualToPoint(view.bounds.origin, CGPointZero)) {
            [xml appendFormat:@" bounds="%@"", NSStringFromCGRect(view.bounds)];
        }
       
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scroll = (UIScrollView *)view;
            if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scroll.contentInset)) {
                [xml appendFormat:@" contentInset="%@"", NSStringFromUIEdgeInsets(scroll.contentInset)];
            }
        }
       
        // 3.判断是否要结束
        if (view.subviews.count == 0) {
            [xml appendString:@" />"];
            return xml;
        } else {
            [xml appendString:@">"];
        }
       
        // 4.遍历所有的子控件
        for (UIView *child in view.subviews) {
            NSString *childXml = [self digView:child];
            [xml appendString:childXml];
        }
       
        // 5.标签结尾
        [xml appendFormat:@"</%@>", view.class];
       
        return xml;
    }


  • 相关阅读:
    HDU 4388 To the moon
    HDU 4757 Tree
    HDU 5816 Hearthstone
    hihocoder 1356 分隔相同整数
    HDU 5726 GCD
    POJ3026 Borg Maze(Prim)(BFS)
    POJ1258 Agri-Net(Prim)
    POJ1751 Highways(Prim)
    POJ2349 Arctic Network(Prim)
    POJ1789 Truck History(Prim)
  • 原文地址:https://www.cnblogs.com/wanghang/p/6298883.html
Copyright © 2020-2023  润新知