Q&A
Xcode 11.4.1
TargetはiOS13.4.1とし、Storyboardを使用せず、ViewControllerを表示させようとしています。
AppDelegate.swift, SceneDelegate.swiftは以下の通りで、SceneDelegateでViewControllerを生成するコードでシミュレータで動作させたところ、
シミュレータ画面の上半分だけがViewになってしまいました。シミュレータ画面全画面をViewとしたいのですが、どうしたらいいのでしょうか。
AppDelegate.swift
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { return true } // MARK: UISceneSession Lifecycle 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>) { } }
SceneDelegate.swift
import UIKit class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let scene = (scene as? UIWindowScene) else { return } window = UIWindow(windowScene: scene) window?.rootViewController = TopViewController() window?.makeKeyAndVisible() } func sceneDidDisconnect(_ scene: UIScene) { } func sceneDidBecomeActive(_ scene: UIScene) { } func sceneWillResignActive(_ scene: UIScene) { } func sceneWillEnterForeground(_ scene: UIScene) { } func sceneDidEnterBackground(_ scene: UIScene) { } }
TopViewController.swift
import UIKit class TopViewController: UIViewController { var bannerView = UIView() var titleView = UIImageView(image: UIImage(named: "tobecontinued.png")) var startButton = UIButton() init() { startButton.setTitle("start", for: UIControl.State.normal) super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.systemBackground //addSubviewはviewDidLoadで view.addSubview(bannerView) view.addSubview(titleView) view.addSubview(startButton) titleView.contentMode = .scaleAspectFit } override func viewDidLayoutSubviews() { // titleViewは真ん中に titleView.frame.size.width = self.view.frame.width titleView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true titleView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true // startボタンはちょっとしたに startButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true startButton.topAnchor.constraint(equalTo: titleView.bottomAnchor, constant: 30).isActive = true } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ }
シミュレータの画面(なぜか上半分だけに表示されている)
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。