对于某些专家来说,这可能很容易,所以。问题是在 Xcode 视觉界面中没有 WKWebView 用于 Storyboard使用,而不是 UIWebview,所以它必须以编程方式创建。我在整个应用程序中使用导航 Controller 。
我在 Storyboard中创建了一个 VIEW 并使用自动布局来约束 0,0,0,0。所以想将 WKWebView 添加到该 View “Daview”中。作为 subview 并完全填充它。我似乎无法弄清楚。
请看下面的代码
class ViewController2: UIViewController ,WKNavigationDelegate {
@IBOutlet weak var navigation: UINavigationItem!
@IBOutlet var daview: UIView!
var restWeb: WKWebView?
@IBOutlet var BottomView: UIView!
// var ActivityIndicator = UIActivityIndicatorView()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
println(" daview bounds in VIEWDIDAPPEAR is \(self.daview.bounds) y Frame \(self.daview.frame)")
restWeb!.bounds = daview.bounds
}
override func viewDidLoad() {
super.viewDidLoad()
/* Create our preferences on how the web page should be loaded */
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
/* Create a configuration for our preferences */
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
/* Now instantiate the web view */
restWeb = WKWebView(frame: self.daview.bounds, configuration: configuration)
let urlString = "http://www.google.com"
println("EL DETAIL URL es \(urlString)")
if let theWebView = restWeb {
println("Existe restWeb")
let urlRest = NSURL(string:urlString)
let requestRest = NSURLRequest(URL: urlRest!)
theWebView.loadRequest(requestRest)
theWebView.allowsBackForwardNavigationGestures = true
theWebView.navigationDelegate = self
self.daview.addSubview(theWebView)
}
} // finish viewdidload
最佳答案
一行答案:将代码从 viewDidLoad() 移动到 viewDidAppear()
说明:我不是专家,但我早年遇到过这个问题。
你的 webview 的问题是你试图在 viewDidLoad() 中设置边界
restWeb = WKWebView(frame: self.daview.bounds, configuration: configuration)
但是,只有当 View 出现时,自动布局才完成。意味着,您尝试在 viewDidLoad 中使用的边界 (self.daview.bounds) 甚至在设置 View 布局之前就为您提供了一个值。
解决方案:正确的自动布局边界在 viewDidAppear 中和之后可用。如果你移动你的代码
WKWebView(frame: self.daview.bounds, configuration: configuration)
从 viewDidLoad() 到 viewDidAppear(),那么你所有的愿望都会成真。
关于uiview - 在具有自动布局的 UIView 中以编程方式 WKwebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32056874/