Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为WKWebView设置约束时崩溃_Ios_Swift_Constraints_Wkwebview - Fatal编程技术网

Ios 为WKWebView设置约束时崩溃

Ios 为WKWebView设置约束时崩溃,ios,swift,constraints,wkwebview,Ios,Swift,Constraints,Wkwebview,包含WebView的类因约束而崩溃。代码如下: class MyCustomCell: UICollectionViewCell, WKNavigationDelegate { @IBOutlet private var myImageView: UIImageView? private var myWebView: WKWebView? public var someProperty: SomeClass? { didSet { someLogic() } }

包含WebView的类因约束而崩溃。代码如下:

class MyCustomCell: UICollectionViewCell, WKNavigationDelegate {

    @IBOutlet private var myImageView: UIImageView?
    private var myWebView: WKWebView?
    public var someProperty: SomeClass? { didSet { someLogic() } }

    func someLogic() {

        if myWebView == nil, let url = URL(string: path) {

            myWebView = WKWebView(frame: CGRect.zero, configuration: WKWebViewConfiguration.gifConfiguration())

            myWebView?.scrollView.isScrollEnabled = false
            myWebView?.scrollView.showsHorizontalScrollIndicator = false
            myWebView?.scrollView.showsHorizontalScrollIndicator = false
            myWebView?.contentMode = .scaleAspectFit
            myWebView?.isUserInteractionEnabled = false
            myWebView?.scrollView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: frame.size.height, right: 0)
            myWebView?.navigationDelegate = self
            let request = URLRequest(url: url)
            myWebView?.load(request)
        }
    }

    // MARK: - WKNavigationDelegate

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

        if imageView != nil {

            webView.alpha = 0
            webView.frame = contentView.frame
            webView.translatesAutoresizingMaskIntoConstraints = false
            contentView.insertSubview(webView, aboveSubview: imageView!)
            contentView.addConstraint(NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: myImageView!, attribute: .top, multiplier: 1, constant: 0))
            contentView.addConstraint(NSLayoutConstraint(item: webView, attribute: .bottom, relatedBy: .equal, toItem: myImageView!, attribute: .bottom, multiplier: 1, constant: 0))
            contentView.addConstraint(NSLayoutConstraint(item: webView, attribute: .leading, relatedBy: .equal, toItem: myImageView!, attribute: .leading, multiplier: 1, constant: 0))
            contentView.addConstraint(NSLayoutConstraint(item: webView, attribute: .trailing, relatedBy: .equal, toItem: myImageView!, attribute: .trailing, multiplier: 1, constant: 0))
            contentView.setNeedsLayout()
            contentView.layoutIfNeeded()

            UIView.animate(withDuration: 1, delay: 0, options: .transitionCrossDissolve, animations: {
                webView.alpha = 1
            }, completion: nil)
        }
    }
以下是日志:

2019-09-04 13:32:40.137078+0300 CultureTrip[27965:741645] [LayoutConstraints]视图层次结构未为 约束:添加到视图时, 约束的项必须是该视图(或视图)的后代 本身)。如果之前需要解决约束,则会崩溃 视图层次结构已组装。打断 -[UIView(UIConstraintBasedLayout)\要调试的视图层次结构UnpreparedForConstraint:]。2019-09-04 13:32:40.138155+0300 CultureTrip[27965:741645][布局限制] 未为约束做好准备的视图层次。约束条件: 容器层次结构:;层= > | > | | ; 层=;contentOffset:{0,0};contentSize:{0,0}; adjustedContentInset:{0,0,0,0}>| | |;图层=>| | | |>
|| | | |>| | | |
|| |>layerID=1“绘图区域根”
|在容器层次结构中找不到视图:>该视图的超级视图:>2019-09-04 13:32:40.150305+0300 CultureTrip[27965:741645]遇到未捕获的异常。全部的 Mixpanel实例已存档。2019-09-04 13:32:40.150512+0300 CultureTrip[27965:741645]*由于未捕获而终止应用程序 异常“NSGenericeException”,原因:“无法安装约束” 正在查看。约束是否引用了来自外部的内容 视图的子树?那是违法的。 约束:视图:;图层=>' *第一次抛出调用堆栈:(0 CoreFoundation 0x00000001158d56fb异常预处理+331 1 libobjc.A.dylib
0x000000011472aac5 objc_异常_抛出+48 2 CoreFoundation
0x000 000 01158D55 55 + [ NSExpExo]:格式:+ 197 3基金会 0x0000000111cb599f-[NSLayoutConstraint _addToEngine:mutuallyExclusiveConstraints:+205 4 UIKitCore 0x000000011d657bbd-[UIView(UIConstraintBasedLayout) _layoutEngine\u添加LayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] +447 5 UIKitCore 0x000000011d657fb0-[UIView(UIConstraintBasedLayout)\尝试在不更新约束阵列的情况下添加约束:舍入调整:相互排斥约束:] +30 6 UIKitCore 0x000000011d658653-[UIView(UIConstraintBasedLayout)nsli\U addConstraint:+38 7 CultureTrip 0x000000010d8262d2 $s11CultureTyp21ExploretileMediumCellc7WebView\u9didnishyso05wkWebH0c\u12wkNavigationCsgtf +1522 8 CultureTrip 0x000000010d826bb1$S11 CultureTrip21探索媒体单元7网络视图-完成了0 5wkWebH0c-So12 wk导航CSGTFTO +81 9 WebKit 0x0000000113e16438 _zn6WebKit 15导航状态16导航客户端19未完成导航\u 12网页ProxyEPN3API10导航PN4_6对象 +92 10 WebKit 0x0000000113e76f84 _zn6WebKit12WebPageProxy21dFinishLoadforFrameyyRkns_8UserDataE+784 11 WebKit 0x0000000114076512 _ZN6WebKit12WebPageProxy17DiReceiveMessageerN3IPC10ConnectionerNs1\u 7Decode +5980 12 WebKit 0x0000000113ce32a6\u ZN3IPC18消息接收者映射15发送消息连接解码 +126 13 WebKit 0x0000000113e99762_zn6WebKit15WebProcessProxy17diReceiveMessageern3IPC10Connectionerns1_7DecoderE +24 14 WebKit 0x0000000113cd61c8\u ZN3IPC10连接15发送消息ENST3\u 110unique\u ptrINS\u 7DecoderENS1\u 14默认值\u deleteIS3\u EEEE +108 15 WebKit 0x0000000113cd8bdd\u ZN3IPC10Connection24dispatchIncomingMessagesEv+717 16 JavaScriptCore 0x000000011f0291f4 _ZN3WTF7RunLoop11performWorkEv+228 17 JavaScriptCore 0x000000011f029482_ZN3WTF7RunLoop11performWorkEv+34 18 CoreFoundation 0x000000011583cbe1 __CFRUNLOOP\u正在调用\u OUT\u到\u A\u SOURCE0\u PERFORM\u函数
+17 19 CoreFoundation 0x000000011583c463 __CFRunLoopDoSources0+243 20 CoreFoundation 0x0000000115836b1f\ucfrunlooprun+1231 21 CoreFoundation
0x0000000115836302 CFRunLoopRunSpecific+626 22图形服务
0x0000000118add2fe GSEventRunModal+65 23 UIKitCore
0x000000011d260ba2 UIApplicationMain+140 24 CultureTrip
0x000000010d74ab1b main+75 25 libdyld.dylib
0x0000000116e6a541启动+1 26???
0x0000000000000001(0x0+1)


使用
contentView.addSubView(myWebView)

在此语句之前,
contentView.insertSubview(webView,subview上面:imageView!)
您正在使用
imageView
contentView
中添加
webView
的后代约束,因此它会由于
imageView
contentView
中找不到而打破约束。因此,请检查
imageView
的位置,然后应用约束。

正如崩溃所说,在添加约束之前,您需要将web视图添加到视图层次结构中。检查视图之间的关系。
imageView
从何而来vs
myImageView
Nice solution@sumeett这并不能解决问题crash@Luda您尝试在层次结构的底层添加
myWebView
?如果没有,请尝试此
contentView.insertSubview(myWebView,at:0)
然后将约束添加到ImageView它位于何处是什么意思?