Set UIViewController view property to custom UIView class without storyboard / nib

前端 未结 4 1936
我在风中等你
我在风中等你 2021-02-07 02:16

I have a UIViewController called LoginViewController. I want to build the view of that LoginViewController fully programmatically<

4条回答
  •  暖寄归人
    2021-02-07 02:48

    In your Viewcontroller's loadView method do this:

    class LoginViewController: UIViewController {
        override func loadView() {
            super.view = LoginView()
        }
    }
    

    In your UIView's custom class do this:

    class LoginView: UIView {
        convenience init() {
            self.init(frame: UIScreen.main.bounds)
            setupLabels()
        }
    }
    

    Now your UIView has a frame , and you can setup all your views through code by providing them frames.

提交回复
热议问题