質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

1975閲覧

【Swift】Xcode 11 でstoryboardを使わずにViewControllerを作ったら上半分のビューになった

sacakoro

総合スコア35

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/04/26 04:56

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. } */ }

シミュレータの画面(なぜか上半分だけに表示されている)
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

自己解決致しました。
addSubViewしたUIViewのサブクラスのプロパティに
.translatesAutoresizingMaskIntoConstraints = false
を設定するだけでした。
設定されてないためにAutoLayoutが正しく機能していなかっただけというオチでした。

投稿2020/04/27 06:40

sacakoro

総合スコア35

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

storyboard学派なので、私の言うことに信憑性はありませんが、
どこか(AppDelegate?)でwindowのframeを設定してあげてください。

https://qiita.com/omochimetaru/items/31df103ef98a9d84ae6b

投稿2020/04/27 06:24

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問