我正在使用81个按钮创建一个数独风格的swift应用程序(按堆栈视图组织)。
我使用标记值0到80引用按钮,它与我用来存储的数组对齐值。但是,第一个按钮导致问题,我认为因为UILayoutGuide对象也有标记0。
此解决方案正在运行,但我刚刚升级到Xcode版本8.0(8A218a)并转换为swift 3.
我将以下代码添加到视图控制器的viewDidLoad函数中:
// Get the subviews of the view
var subviews = view.subviews
// Return if there are no subviews
if subviews.count == 0 {
return
}
for subview : AnyObject in subviews{
// Do what you want to do with the subview
print("\(subview.tag) - \(subview)")
}
这会产生以下日志:
可选(200) - UIImageView:0x7ff3e240f3b0; frame =(-4 0; 383 667); autoresize = RM + BM; userInteractionEnabled = NO; tag = 200; layer =
可选(200) - UIStackView:0x7ff3e240f590; frame =(7 103; 360 360); opaque = NO; autoresize = RM + BM; tag = 200; layer = CATransformLayer:0x6100002316a0
可选(200) - UIImageView:0x7ff3e24111b0; frame =(0 47; 375 436); autoresize = RM + BM; userInteractionEnabled = NO; tag = 200; layer = CALayer:0x6100002344e0
可选(200) - UIToolbar:0x7ff3e2637ff0; frame =(0 623; 375 44); opaque = NO; autoresize = RM + BM; tag = 200; layer = CALayer:0x6000000343e0
可选(200) - UIImageView:0x7ff3e26015f0; frame =(63 228; 248 211); autoresize = RM + BM; userInteractionEnabled = NO; tag = 200; layer = CALayer:0x600000034ea0
可选(200) - UILabel:0x7ff3e263e2e0; frame =(117 313; 141 41); text ='好工作'; opaque = NO; autoresize = RM + BM; userInteractionEnabled = NO; tag = 200; layer = _UILabelLayer:0x600000093060
可选(200) - UIStackView:0x7ff3e263e750; frame =(127 491; 120 120); opaque = NO; autoresize = RM + BM; tag = 200; layer = CATransformLayer:0x600000035520
可选(0) - _UILayoutGuide:0x7ff3e263ee80; frame =(0 0; 0 0); hidden = YES; layer = CALayer:0x600000035bc0
可选(0) - _UILayoutGuide:0x7ff3e263f030; frame =(0 0; 0 0); hidden = YES; layer = CALayer:0x600000035c60
所以最后两行输出是我怀疑的UILayoutGuide对象。我没有对它们进行编码,它们不是层次结构中的对象。我怎么看他们/摆脱他们?
答案 0 :(得分:2)
如果您只是想阻止循环添加_UILayoutGuide(属于UILayoutSupport类),请执行以下操作:
// Get the subviews of the view
var subviews = view.subviews
// Return if there are no subviews
if subviews.count == 0 {
return
}
for subview : AnyObject in subviews{
// Do what you want to do with the subview
print("\(subview.tag) - \(subview)")
// Check for whether is it a UILayout class or not
if(subview is UILayoutSupport){
//This section is executed when UILayoutGuide will come into play
print("Don't add it to the stack")
}else{
// Write your code in this section
}
}
您也可以打印堆栈,检查其中的UILayoutGuide并将其删除。
答案 1 :(得分:0)
简单的回答是UILayoutGuides没有标签。当您打印subview.tag时,如果没有标记,则显示tag = 0。所以按钮0的问题与UILayoutGuide无关。
我没有弄清楚标签0按钮的问题,但重新编码以避免使用标签0.不完全修复但解决方案有效。