iOS UIViewの階層構造をprintする extension by Swift 3.0


隠れているUIViewを確認するための便利関数を自作

Utility.swift
import Funcation
import UIKit

extension UIWindow {
    func printSubviewHierarchy() {
        var tabs:String = ""
        printSubviews(subviews: self.subviews, tabs: &tabs)
    }

    private func printSubviews(subviews: [UIView], tabs:inout String) {
        tabs += " "
        for view in subviews {
            print(tabs + "\(view.self)")
            printSubviews(subviews: view.subviews, tabs: &tabs)
        }
        tabs.remove(at: tabs.index(before: tabs.endIndex))
    }
}

使用例

ViewController.swift
    @IBAction func tapped(_ sender: UIButton){
        self.view.window?.printSubviewHierarchy()
    }

結果表示例


DebugWindow
 <UILayoutContainerView: ...
  <UINavigationTransitionVIew: ...
    :