首页 > 解决方案 > WKWebView - 在横向视图中忽略正确的“安全区域插图”

问题描述

我对 iOS 开发相当陌生,我正在使用 WKWebView 来呈现我的网站。我正在尝试添加一些约束,但想忽略屏幕截图中标记的横向视图中正确的“安全区域插图”。

这是屏幕截图。

在此处输入图像描述

这是代码。

let webConfiguration = WKWebViewConfiguration()

let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.webViewContainer.frame.size.height))
     
self.webView = WKWebView (frame: customFrame , configuration: webConfiguration)
       webView.translatesAutoresizingMaskIntoConstraints = false
       self.webViewContainer.addSubview(webView)
    
    
addConstraints(to: webView, with: webViewContainer)

func addConstraints(to webView: UIView, with superView: UIView) {
    webView.translatesAutoresizingMaskIntoConstraints = false
    let leadingConstraint = NSLayoutConstraint(item: webView, attribute: .leading, relatedBy: .equal, toItem: superView, attribute: .leading, multiplier: 1, constant: 0)
    let trailingConstraint = NSLayoutConstraint(item: webView, attribute: .trailing, relatedBy: .equal, toItem: superView, attribute: .trailing, multiplier: 1, constant: 0)
    let topConstraint = NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: superView, attribute: .top, multiplier: 1, constant: 0)
    let bottomConstraint = NSLayoutConstraint(item: webView, attribute: .bottom, relatedBy: .equal, toItem: superView, attribute: .bottom, multiplier: 1, constant: 0)
    superView.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])
}

任何帮助将不胜感激。

标签: iosswiftwkwebview

解决方案


使用尺寸类并更改约束,如下图所示

在此处输入图像描述


推荐阅读