如果您有多个 UIView,通常情况下,您可以通过在索引处使用 insertSubview 来堆叠它们。
self.view.insertSubview(testUIView, atIndex: 1)
但我想使用单个 UIView。
我有一系列不同的 UIView
let plotUIViews: [UIView] = [testView(), FaceView()]
这里是测试 View
import UIKit
class testView: UIView {
override func drawRect(rect: CGRect) {
var greenView = UIView(frame: CGRectMake(100, 200, 100, 100))
greenView.backgroundColor=UIColor.greenColor()
}
}
我有一个通用的 UIView
@IBOutlet var testUIView: UIView!
并且想这样做,它有点 perl-ish 而不是 swift-ish(为了节省在每一层上绘制所有 View 然后操作索引的内存)
self.testUIView?.removeFromSuperview()
self.testUIView = nil
switch plotValue{
case 0:
self.testUIview=plotUIViews[0]
case 1:
self.testUIview=plotUIViews[1]
case 2:
self.testUIview=plotUIViews[2]
etc
}
self.view.insertSubview(testUIView, atIndex: 2)
testUIView 中没有任何显示
感谢任何帮助或指点。
最佳答案
听起来您想要的是一次只显示一个绘图 View 。因此,在显示新 View 之前删除之前的绘图 View (如果有的话):
self.testUIView?.removeFromSuperview()
self.testUIView = nil
switch plotValue {
...
}
self.view.insertSubview(testUIView, atIndex: 2)
关于ios - 没有堆叠的多个 UIView 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28485732/