答案 0 :(得分:4)
您可以查看该特定视图中的子视图数量:
if([theView.subviews count] == 0) {
// View does not contain subviews
}
如果您在父视图中有多个UIViews,并且您希望找出哪些视图为空,那么循环遍历父视图并检查每个视图是否为空:
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.
}
}
答案 1 :(得分:1)
试试这个:
isViewEmpty
将扩展代码粘贴到viewController类之外。
从视图中删除按钮后,每次都会检查//if you don't have the object of view, you can get view as below,
let view = bottonToRemove.superview;//this will give you obejct for check
//your code to remove button from the view
if view.isViewEmpty {
//implement your logic for if view is empty
}else{
//view not empty
//do you stuff
}
,如下所示
geturl()
答案 2 :(得分:0)
UIView有一个属性subViews。这将返回所有子视图的数组。如果数组为null或计数为零,则其中没有子视图。
答案 3 :(得分:-1)
我认为根据您的要求,您需要检查视图中是否有任何按钮,您可以尝试
BOOL isEmpty = true;
for (UIButton *btn in viewTest.subviews) {
if ([btn isKindOfClass:[UIButton class]]) {
isEmpty = false;
break;
}
}
if (isEmpty == false) {
// there is button in view
}
else
{
// there no button in view
}
答案 4 :(得分:-2)
好像你正在检查UINavigationBar,
for (UIView *view in self.navigationController.navigationBar.subviews) {
if(view)
//Do your thing
}