ios swift uistackview 更新子项高度和x、y帧位置值
ios swift uistackview update children height and x, y frame position values
我有一个布局如下的表格视图单元格 -
ContentView
- StackView
- Fixed Height UILabel
- UIView (Webview Container)
- Fixed Height UILabel
- ... more views
现在我想像这样在这个 UIView 中添加一个 webview -
func prepareQuestionWebView() {
let webConfiguration = WKWebViewConfiguration()
let contentController = WKUserContentController();
webConfiguration.userContentController = contentController
webView = WKWebView(frame: CGRect(origin: CGPoint.zero, size: questionView.frame.size))
webView?.scrollView.isScrollEnabled = false
webView?.scrollView.bounces = false
webView?.scrollView.bouncesZoom = false
webView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView?.translatesAutoresizingMaskIntoConstraints = false
webView?.tag = -1
webView?.navigationDelegate = self
questionView.removeAllSubviews()
questionView.addSubview(webView!)
webView?.contentScaleFactor = 1
}
我为堆栈视图和 webview 容器创建了以下出口 -
@IBOutlet weak var questionView: UIView!
@IBOutlet weak var rootStack: UIStackView!
并使 questionView 框架大小等于 webview 的内容大小我正在这样做 -
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (result, error) in
if let height = result as? CGFloat {
self.setHeightData(height, webView)
}
})
}
}
}
func setHeightData(_ height: CGFloat, _ wv: WKWebView) {
switch wv.tag {
case -1:
questionView.frame = CGRect.init(x: 0, y: 36, width: rootStack.frame.width, height: height)
default:
break
}
wv.layoutSubviews()
questionView.layoutIfNeeded()
rootStack.layoutSubviews()
rootStack.layoutIfNeeded()
}
这会更新 webview 大小及其容器的大小,但由于某些原因,stackview 中 webview 容器之后的元素未更新,因此它们与 webview 容器重叠
那么,在将 questionView 的高度设置为其 webview 的内容后,如何更新 stackview 视图?
或者是否有另一种方法可以使问题视图(webview 容器)的高度根据其内容动态变化,并使其余视图在其高度变化时向下移动?
您已使用自动布局设置堆栈视图和其他视图的大小,并且您正在使用框架设置网络视图和容器视图的大小。正因为如此,stackview和它里面的其他view是无法调整的。与其给 webview 的容器一个框架,不如将它的四壁约束到 webview 的四壁,并给 webview 一个高度约束。
我有一个布局如下的表格视图单元格 -
ContentView
- StackView
- Fixed Height UILabel
- UIView (Webview Container)
- Fixed Height UILabel
- ... more views
现在我想像这样在这个 UIView 中添加一个 webview -
func prepareQuestionWebView() {
let webConfiguration = WKWebViewConfiguration()
let contentController = WKUserContentController();
webConfiguration.userContentController = contentController
webView = WKWebView(frame: CGRect(origin: CGPoint.zero, size: questionView.frame.size))
webView?.scrollView.isScrollEnabled = false
webView?.scrollView.bounces = false
webView?.scrollView.bouncesZoom = false
webView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView?.translatesAutoresizingMaskIntoConstraints = false
webView?.tag = -1
webView?.navigationDelegate = self
questionView.removeAllSubviews()
questionView.addSubview(webView!)
webView?.contentScaleFactor = 1
}
我为堆栈视图和 webview 容器创建了以下出口 -
@IBOutlet weak var questionView: UIView!
@IBOutlet weak var rootStack: UIStackView!
并使 questionView 框架大小等于 webview 的内容大小我正在这样做 -
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (result, error) in
if let height = result as? CGFloat {
self.setHeightData(height, webView)
}
})
}
}
}
func setHeightData(_ height: CGFloat, _ wv: WKWebView) {
switch wv.tag {
case -1:
questionView.frame = CGRect.init(x: 0, y: 36, width: rootStack.frame.width, height: height)
default:
break
}
wv.layoutSubviews()
questionView.layoutIfNeeded()
rootStack.layoutSubviews()
rootStack.layoutIfNeeded()
}
这会更新 webview 大小及其容器的大小,但由于某些原因,stackview 中 webview 容器之后的元素未更新,因此它们与 webview 容器重叠
那么,在将 questionView 的高度设置为其 webview 的内容后,如何更新 stackview 视图?
或者是否有另一种方法可以使问题视图(webview 容器)的高度根据其内容动态变化,并使其余视图在其高度变化时向下移动?
您已使用自动布局设置堆栈视图和其他视图的大小,并且您正在使用框架设置网络视图和容器视图的大小。正因为如此,stackview和它里面的其他view是无法调整的。与其给 webview 的容器一个框架,不如将它的四壁约束到 webview 的四壁,并给 webview 一个高度约束。