loadView()
メソッドでカスタムビューをプログラムで設定しています。 AppDelegate
のルートビューコントローラーを指定されたビューコントローラーに変更している間に、表示されていても、ビューコントローラーにサブレイアウトが正しくレイアウトされて表示されていないことに気付きました。ビューデバッガを使用すると、ビューはビュー階層内にあるとみなされますが、それらは見つからないため、空のビューコントローラで終わります。奇妙なのは、別のビューコントローラーからビューコントローラーを表示すると、そのビューコントローラーが正しく配置され、すべてのビューが表示されるということです。私のビューコントローラはすべて、以下に示す同じ作成に従います。 ルートビューコントローラとしてプログラムで設定したとき、View Controllerがサブビューを正しく表示しない
class ViewController: UIViewController {
private var rootView: View? {
get { return self.viewIfLoaded as? View ?? .none }
set (view) { self.view = view }
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init() {
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func loadView() {
self.rootView = View()
}
}
そして、ここでここで私は私の見解コントローラの私のカスタムビューを作成しています方法です
class View: UIView {
// Lazy declare views here
fileprivate var shouldSetupConstraints = true
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
override func updateConstraints() {
if(shouldSetupConstraints) {
// Adding constraints here
shouldSetupConstraints = false
}
super.updateConstraints()
}
func setupView() {
// Adding subviews here
}
}
は、私は私のAppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.backgroundColor = Palette.backgroundColor
window?.rootViewController = MyViewController()
window?.makeKeyAndVisible()
return true
}
で、ルートビューコントローラを設定しています方法です
プログラムでレイアウトされたビューコントローラがルートとして設定されている間に適切に表示されることを期待していますビューコントローラは表示されず、適切に配置されません。ビューコントローラが別のビューコントローラによって表示されている場合にのみ、これらは正しく表示されます。