swift-获取View的指定子视图

通过递归获取指定view的所有子视图.

  1. 获取View的子视图
    使用
    1
    2
    let subViewArr = view.getAllSubViews() // 获取所有子视图
    let imageViewArr = view.getSubView(name: "UIImageView") // 获取指定类名的子视图

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
extension UIView {
private static var getAllsubviews: [UIView] = []
public func getSubView(name: String) -> [UIView] {
let viewArr = viewArray(root: self)
UIView.getAllsubviews = []
return viewArr.filter {$0.className == name}
}
public func getAllSubViews() -> [UIView] {
UIView.getAllsubviews = []
return viewArray(root: self)
}
private func viewArray(root: UIView) -> [UIView] {
for view in root.subviews {
if view.isKind(of: UIView.self) {
UIView.getAllsubviews.append(view)
}
_ = viewArray(root: view)
}
return UIView.getAllsubviews
}
}

extension NSObject {
var className: String {
let name = type(of: self).description()
if name.contains(".") {
return name.components(separatedBy: ".")[1]
} else {
return name
}
}
}
  1. 获取UIAlertController的titleLabel和messageLabel
    UIAlertController好用,但可自定义程度不高,例如我们想让message文字左对齐,就需要获取到messageLabel,但UIAlertController并没有提供这个属性.

我们就可以通过递归拿到alertTitleLabel和alertMessageLabel.

1
2
3
4
5
6
7
8
extension UIAlertController {
public var alertTitleLabel: UILabel? {
return self.view.getSubView(name: "UILabel").first as? UILabel
}
public var alertMessageLabel: UILabel? {
return self.view.getSubView(name: "UILabel").last as? UILabel
}
}