Manually Created UIWindow is Wrong Size
Asked Answered
D

3

11

I am learning how to create an iOS app without Interface Builder (i.e. storyboards, xibs, etc.). Below is the basic app delegate class I am working with. The problem is that when displayed the UIWindow does not use up the full screen of the device (see attached screenshot). This occurs on all the devices and simulators I test with. Why isn't the fullscreen being used?

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  lazy var window: UIWindow? = {
    debugPrint("AppDelegate: Creating Window")

    let screen: UIScreen = UIScreen.mainScreen()
    let window: UIWindow = UIWindow.init(frame: screen.bounds)

    window.backgroundColor = UIColor.whiteColor()

    return window
  }()

  lazy var rootViewController: ShapesViewController = {
    debugPrint("AppDelegate: Creating Root View Controller")

    let rootViewController = ShapesViewController.init(nibName: nil, bundle: nil)

    return rootViewController
  }()

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    debugPrint("AppDelegate: applicationWillEnterForeground")

    window?.rootViewController = rootViewController
    window?.makeKeyAndVisible()

    return true
  }
}

Why doesn't the window use the whole screen?

Decarbonize answered 7/8, 2016 at 19:26 Comment(0)
D
16

The problem turned out to be that I did not have a launch image or storyboard. For some reason without this the app defaults to the 3.5" size. Credit to this comment: A launch storyboard or xib must be provided unless the app requires full screen

Decarbonize answered 7/8, 2016 at 19:49 Comment(1)
To quote the book "iOS 9 Programming Fundamentals with Swift" by Matt Neuberg (amazing tech writer!): The presence of a Launch screen interface file base name key in your Info.plist tells the system that your app runs natively on newer device types, such as the iPhone 6 and iPhone 6 Plus. Without it, your app will be displayed zoomed … you won't be getting all the pixels you're entitled to …Messer
S
2

This always works for me (I don't see any clear difference though). Do you mind sharing your ShapesViewController code as well?

Updated to swift 4.2

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = ViewController()
    window?.makeKeyAndVisible()
    return true
}
Stewart answered 7/8, 2016 at 19:36 Comment(2)
The ShapesViewController is empty. I even changed the root view controller to a plain old UIViewController object and I got the same thing. I tried your sample code just to rule out any weirdness and it looks the same.Decarbonize
@macinjosh Maybe you should try out the sample code in a new plain project. Also did you delete the main.storyboard file?Stewart
F
0

I know sounds silly but... For a fresh project what macinjosh is saying works. For a project of mine in Xcode 8 and swift 3, it didn't (probably because I deleted the launch screen and had to create again). Just created new project fresh, and follow what macinjosh said. Then migrate all your files back in.

Feverous answered 23/10, 2016 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.