So I've created a new iOS single view application with no core data, no test and then I deleted Main.storyboard and the launch screen storyboard. I want to do as much in code as I can so I can understand what I'm doing (since I'm just learning iOS programming). The thing is, that I just cant fill the whole iPhone X screen with a background image. I've tried this first using storyboards and had no problem whatsoever. Am I missing something?
// AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainController = MainController()
window?.rootViewController = mainController
window?.makeKeyAndVisible()
return true
}
}
// MainController.swift
import UIKit
class MainController: UIViewController {
let background: UIImageView = {
let image = UIImageView(image: #imageLiteral(resourceName: "bg"))
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
}
extension MainController {
override func viewDidLoad() {
super.viewDidLoad()
setupBackground()
}
}
extension MainController {
func setupBackground() {
view.addSubview(background)
NSLayoutConstraint.activate([
background.leadingAnchor.constraint(equalTo: view.leadingAnchor),
background.trailingAnchor.constraint(equalTo: view.trailingAnchor),
background.topAnchor.constraint(equalTo: view.topAnchor),
background.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
}
To summarize the comments above, you need to use a launch storyboard in order for iOS to give you a UIScreen that covers the device's full bounds.
If you don't already have a launch storyboard, create one in Xcode 11 via File
> New
> File...
> Launch Screen
. Then make sure to select that launch storyboard under your app target > General > Launch Screen File.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.