我在一个空洞的新项目中。
我添加了一个名为MyCustomView的自定义视图:
import UIKit
public class MyCustomView: UIView{
private var littleView: UIView!
open class func show() -> UIView{
let bigView = MyCustomView()
bigView.configureView()
UIApplication.shared.keyWindow?.addSubview(bigView)
return bigView
}
private func configureView(){
let screenSize = UIScreen.main.bounds.size
self.frame = CGRect(x: 0,
y: 0,
width: screenSize.width,
height: screenSize.height)
littleView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
littleView.backgroundColor = .black
addSubview(littleView)
}
}
在ViewController中执行此操作:
override func viewDidLoad() {
let test = MyFirstView.show()
}
我希望这会显示视图,但我仍然需要使用self.view.addSubview(test)
才能看到它....
我想到了UIApplication.shared.keyWindow?.addSubview(bigView)
并向其添加了一个子视图,它应该呈现视图。
我错过了什么?
答案 0 :(得分:0)
在viewDidAppear
override func viewDidAppear() {
let test = MyFirstView.show()
}