我才刚刚开始学习Swift,并且正在尝试使用Interface Builder构建基本的Web浏览器。我遇到的问题是,一旦将WKWebView嵌入堆栈视图中,它就会停止显示任何内容(以前称为stackWebView)。如果Web视图位于堆栈外部,位于ViewController内部(称为topWebView),则它将按预期工作。这是我到目前为止编写的代码。我知道有一些解决方案可以通过编程方式完成,但是我试图理解Interface Builder和代码之间的交互。那么如何在堆叠的Webview中显示内容?
import UIKit
import WebKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var urlName: UITextField!
@IBOutlet weak var stackWebView: WKWebView!
@IBOutlet weak var topWebView: WKWebView!
private func loadWebPage(_ s: String){
topWebView.load(URLRequest(url: URL(string: s)!))
stackWebView.load(URLRequest(url: URL(string: s)!))
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
urlName.text = textField.text
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
urlName.text = textField.text
loadWebPage(urlName.text!)
}
override func viewDidLoad() {
super.viewDidLoad()
urlName.delegate = self
let startURL = URL(string: "https://www.duckduckgo.com")
topWebView.load(URLRequest(url: startURL!))
stackWebView.load(URLRequest(url: startURL!))
}
}
答案 0 :(得分:1)
请找到以下适合我的代码。甚至可以通过在文本字段中输入url来以堆栈视图加载Web视图
代码
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, UITextFieldDelegate {
@IBOutlet weak var urlName: UITextField!
@IBOutlet weak var stackWebView: WKWebView!
@IBOutlet weak var topWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
urlName.delegate = self
topWebView.navigationDelegate = self
stackWebView.navigationDelegate = self
let startURLString = "https://www.duckduckgo.com"
loadWebPage(startURLString: startURLString)
}
private func loadWebPage(startURLString: String){
var formattedURLString = startURLString
if !formattedURLString.lowercased().contains("http") {
formattedURLString = "http://\(formattedURLString)"
}
let startURL : URL = URL(string: formattedURLString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)!
print(startURL)
topWebView.load(URLRequest(url: startURL))
stackWebView.load(URLRequest(url: startURL))
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
loadWebPage(startURLString: urlName.text!)
return true
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
}
}
故事板
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="StackViewExample" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="dCG-SE-8O1">
<rect key="frame" x="32" y="52" width="311" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="YQA-bf-cZ2"/>
</constraints>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="BYZ-38-t0r" id="81P-xI-VQI"/>
</connections>
</textField>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="naB-Di-vk8">
<rect key="frame" x="0.0" y="114" width="375" height="553"/>
<subviews>
<wkWebView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="TzL-qx-9oE">
<rect key="frame" x="0.0" y="0.0" width="375" height="271.5"/>
<color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<wkWebViewConfiguration key="configuration">
<audiovisualMediaTypes key="mediaTypesRequiringUserActionForPlayback" none="YES"/>
<wkPreferences key="preferences"/>
</wkWebViewConfiguration>
</wkWebView>
<wkWebView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="8Th-zS-htL">
<rect key="frame" x="0.0" y="281.5" width="375" height="271.5"/>
<color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<wkWebViewConfiguration key="configuration">
<audiovisualMediaTypes key="mediaTypesRequiringUserActionForPlayback" none="YES"/>
<wkPreferences key="preferences"/>
</wkWebViewConfiguration>
</wkWebView>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="dCG-SE-8O1" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="32" id="6ud-iU-788"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="naB-Di-vk8" secondAttribute="bottom" id="I9W-Y2-PPp"/>
<constraint firstItem="naB-Di-vk8" firstAttribute="top" secondItem="dCG-SE-8O1" secondAttribute="bottom" constant="32" id="Muf-dU-Xgl"/>
<constraint firstItem="naB-Di-vk8" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="X7T-e4-rEF"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="naB-Di-vk8" secondAttribute="trailing" id="iod-JL-8w5"/>
<constraint firstItem="dCG-SE-8O1" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="32" id="oQT-3t-opj"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="dCG-SE-8O1" secondAttribute="trailing" constant="32" id="qtr-49-LHO"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="stackWebView" destination="8Th-zS-htL" id="Iie-Yi-yyB"/>
<outlet property="topWebView" destination="TzL-qx-9oE" id="wLl-yX-yj7"/>
<outlet property="urlName" destination="dCG-SE-8O1" id="jhS-ly-gdy"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
答案 1 :(得分:0)
必须对webView有四个约束。 Orelse它的父级应该有四个约束