模拟器中不会发生此错误,但会在手机上发生。我有5个不同的webViews,我有一个菜单设置,以便当选择一个菜单时,它将被添加为子视图,其他webViews将从superview中删除。
// MARK: - Hides all other webviews and shows selected
func hideOtherWebViews(selectedWebView: WKWebView!){
self.navBarFixed = false
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if checkLoaded.currentWebView != self.selectedWeb{
for webview in [self.facebookWeb, self.instagramWeb, self.googleplusWeb, self.twitterWeb, self.youtubeWeb, self.linkedinWeb]{
// If it isn't the selected web view do this
if webview != selectedWebView{
webview.removeFromSuperview()
println("web hidden")
// If it is the selected web view do this
}else{
self.centerView.addSubview(webview)
webview.setTranslatesAutoresizingMaskIntoConstraints(false)
//webview constraints
let top = NSLayoutConstraint(item: webview, attribute: .Top, relatedBy: .Equal, toItem: webview.superview, attribute: .Top, multiplier: 1, constant: 0)
let right = NSLayoutConstraint(item: webview, attribute: .Right, relatedBy: .Equal, toItem: webview.superview, attribute: .Right, multiplier: 1, constant: 0)
let left = NSLayoutConstraint(item: webview, attribute: .Left, relatedBy: .Equal, toItem: webview.superview, attribute: .Left, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: webview, attribute: .Bottom, relatedBy: .Equal, toItem: webview.superview, attribute: .Bottom, multiplier: 1, constant: 0)
self.view.addConstraints([ top, right, left, bottom])
webview.scrollView.delegate = self
webview.navigationDelegate = self
webview.UIDelegate = self
webview.scrollView.showsHorizontalScrollIndicator = false
webview.scrollView.showsVerticalScrollIndicator = false
self.whiteOverlay.alpha = 1
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.whiteOverlay.alpha = 0
})
}
}
}
})
错误发生在
上self.centerView.addSubview(webview)