前提・実現したいこと
Storyboard,xibを使用せずにコードのみでアプリを作成しようとしているのですが、
アプリを立ち上げた際に画面が真っ黒の状態になってしまいます。
こちらの解決方法についてご教示いただければ幸いです。
発生している問題・エラーメッセージ
Storyboard 抜きで、コードオンリーで iOS アプリの UI を作る
こちらの記事のソースコードをそのまま使って作成してみたのですが、アプリを立ち上げると真っ黒の状態になってしまいました。
デバックエリアには以下のように表示されておりました。
NoStoryBoard[15434:452943] [WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
RootviewControllerの設定の部分でうまくいっていないせいだとは思うのですが、どのように修正すれば良いのかがわからない状態です。
知恵をお貸しいただければと思います。
ソースコード
AppDelegate
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? private(set) lazy var viewController = ViewController() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.makeKeyAndVisible() self.window?.rootViewController = viewController return true } func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { } }
ViewController
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let mainView = MainView(frame: self.view.bounds) mainView.autoresizingMask = [.flexibleWidth, .flexibleHeight] self.view.addSubview(mainView) } }
MainView
import UIKit class MainView: UIView { let mainLabel: UILabel override init(frame: CGRect) { self.mainLabel = UILabel() self.mainLabel.text = "Hello World!" self.mainLabel.textAlignment = .center super.init(frame: frame) self.backgroundColor = .white self.addSubview(mainLabel) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func layoutSubviews() { super.layoutSubviews() let labelSize = self.mainLabel.sizeThatFits(self.bounds.size) let x = (self.bounds.width - labelSize.width) / 2 let y = (self.bounds.height - labelSize.height) / 2 let labelOrigin = CGPoint(x: x, y: y) self.mainLabel.frame = CGRect(origin: labelOrigin, size: labelSize) } }
まだ回答がついていません
会員登録して回答してみよう