Kir*_*ski 4 swift ios11 safearealayoutguide iphone-x xcode9.2
\n\n*看上面的图片链接,支持iOS9到iOS11
\n\n以下是工作代码解决方案:
\n\nimport UIKit\nimport WebKit\n\nclass ViewController: UIViewController, WKUIDelegate, UIApplicationDelegate, WKNavigationDelegate {\n\n @IBOutlet weak var webViewContainer: UIView!\n\n let requestURLString = "http://google.com/\xe2\x80\x9c\n\n var webView: WKWebView!\n\n override func viewDidLoad() { \n\n super.viewDidLoad()\n let webConfiguration = WKWebViewConfiguration()\n\n let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.webViewContainer.frame.size.height))\n self.webView = WKWebView (frame: customFrame , configuration: webConfiguration)\n webView.translatesAutoresizingMaskIntoConstraints = false\n self.webViewContainer.addSubview(webView)\n webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor).isActive = true\n webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor).isActive = true\n webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor).isActive = true\n webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor).isActive = true\n webView.heightAnchor.constraint(equalTo: webViewContainer.heightAnchor).isActive = true \n\n webView.uiDelegate = self\n webView.navigationDelegate = self\n webView.scrollView.bounces = false \n self.openUrl()\n }\n\n func openUrl() {\n let url = URL (string: requestURLString)\n let request = URLRequest(url: url!)\n webView.load(request)\n\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我正在分享包含此项目的链接:\n https://github.com/kiril6/iPhoneX-webkitWebView
\n\n来源: http: //delovski.net/webkitwebview/
\nclass ViewController:UIViewController,WKNavigationDelegate,WKUIDelegate
{
@IBOutlet weak var webViewContainer: UIView!
fileprivate var requestURLString: URL?
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
loadUrl()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadUrl() {
requestURLString = URL(string: "https://www.apple.com/in/")!
guard let url = requestURLString else { return }
webView = WKWebView()
webView.navigationDelegate = self
webView.allowsLinkPreview = true
webView.uiDelegate = self
webViewContainer.addSubview(webView)
webViewContainer.sendSubview(toBack: webView)
addConstraints(to: webView, with: webViewContainer)
webView.load(NSURLRequest(url: url) as URLRequest)
}
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])
}
}
Run Code Online (Sandbox Code Playgroud)
希望这对您有用,请检查。另外,我正在分享包含在 Swift 4.0 中使用 WKWebView 加载 URL 的项目的链接: https: //github.com/ipran/WebViewTestApp
| 归档时间: |
|
| 查看次数: |
6271 次 |
| 最近记录: |