0

应用程序在所有模拟器上加载全屏。当我将它加载到我的 iPhone X 上时,它是全屏的。然而,在我妻子的 XS Max 上,我得到了下面的信箱。请帮忙!

XS Max 截图

这是我的代码。这是一个非常简单的 WebView 应用程序。

应用委托

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    window = UIWindow()
    window?.rootViewController = ViewController()
    window?.makeKeyAndVisible()
    
    return true
}

}

视图控制器

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

// MARK: - Lifecycle Methods
override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
    let myURL = URL(string: "https://cotta.work")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
}

// MARK: - Properties
lazy var webView: WKWebView = {
    let webConfiguration = WKWebViewConfiguration()
    let webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    webView.translatesAutoresizingMaskIntoConstraints = false
    return webView
}()

}

extension ViewController {
func setupUI() {
    self.view.addSubview(webView)
    
    NSLayoutConstraint.activate([
        webView.topAnchor
            .constraint(equalTo: self.view.topAnchor),
        webView.leftAnchor
            .constraint(equalTo: self.view.leftAnchor),
        webView.bottomAnchor
            .constraint(equalTo: self.view.bottomAnchor),
        webView.rightAnchor
            .constraint(equalTo: self.view.rightAnchor)
    ])
}

}
4

1 回答 1

0

尝试像这样设置根视图控制器,这是针对 AppDelegate 的:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    window = UIWindow()
    let controller = ViewController()
    window?.rootViewController = controller
    window?.makeKeyAndVisible()
    
    return true
}

这适用于 SceneDelegate:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }
    let window = UIWindow(windowScene: windowScene)
    let controller = ViewController()
    window.rootViewController = controller
    self.window = window
    window.makeKeyAndVisible()
}

之后设置您的视图控制器:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

lazy var webView: WKWebView = {
    let webConfiguration = WKWebViewConfiguration()
    let webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    webView.translatesAutoresizingMaskIntoConstraints = false
    return webView
}()

override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
    guard let myURL = URL(string: "https://cotta.work") else {return}
    let myRequest = URLRequest(url: myURL)
    webView.load(myRequest)
}

func setupUI() {
    view.addSubview(webView)
    webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    webView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    webView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
 }
}

自然,如果您在场景委托中呈现 rootViewController,则在 AppDelegate 中删除呈现...

于 2020-12-08T09:24:54.007 回答