Swift纯代码构建VC

纯代码构建VC

以IOS13之后举例(IOS13要用SceneDelegate)

  1. SceneDelegate的scene(willConnectTo)重载方法:
if let windowScene = scene as? UIWindowScene {
    self.window = UIWindow(windowScene: windowScene)
    self.window?.rootViewController = TestOnlyCodeViewController()
    self.window?.backgroundColor = UIColor.white//不设置就是黑的
    self.window?.makeKeyAndVisible()
}
  1. 创建TestOnlyCodeViewController,不创建对应xib:
class TestOnlyCodeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let screen = UIScreen.main.bounds
        let labelWidth:CGFloat = 90
        let labelHeight:CGFloat = 20
        let labelTopView:CGFloat = 150
        let frame = CGRect(x: (screen.size.width - labelWidth)/2, y: labelTopView, width: labelWidth, height: labelHeight)
        let label = UILabel(frame: frame)
        label.text = "test only code show view"
        
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)
        
    }
}

你可能感兴趣的:(IOS开发,swift,ios,开发语言)