ios - 如何检查UIView是否为空

标签 ios swift uiview uibutton swift2

我有多个 UIView,然后在最后一次想要检查 UIView 上的添加和删除按钮后我的 View 是否为空。

enter image description here

上图第三个 View 为空如何以编程方式检查 该 View 是否为空。

最佳答案

您可以检查该特定 View 中的 subview 数量:

if([theView.subviews count] == 0) {
    // View does not contain subviews
}

如果父 View 中有多个 UIView,并且您希望找出哪些 View 为空,则循环遍历父 subview 并检查每个 View 是否为空:

for(UIView * view in parentView.subviews) {
    if([view isKindOfClass:[UIView class]] && [view.subviews count] == 0) {
        // We found an empty UIView... 
        // Can you identify this view?
        // If you need to do something with it, do it here.
    }
}

关于ios - 如何检查UIView是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34542016/

相关文章:

ios - 如何以编程方式设置字体大小以适应 swift 4 中的行数值

ios - 调用 deleteRowsAtIndexPaths 时 UITableView 断言失败

ios - NSNotificationCenter 发送到实例的无法识别的选择器

ios - 如何将 UIViewPropertyAnimator 与自动布局一起使用?

ios - 如何固定图像在 UIButton 中的位置?

python - Python Decimal(32) 在 Swift 中相当于什么?

ios - 使用 View Controller 自定义 UIView 旋转

iOS:了解 UIView 上的开始动画和动画结束

ios - Swift UIViewController Custom init() - 错误无法分配给自己

objective-c - 阻止 UIGestureRecognizer 对 UIView 内的 UIButtons 进行操作