可可窗口在替换视图时的大小加倍
问题描述:
我想在运行时用另一个视图(及其ViewController)替换一个视图。现在,当我这样做时,旧视图会从窗口中消失,但是新视图不会出现,并且窗口变为其原始宽度的两倍。 如果我切换到全屏模式,我可以在应用程序窗口的左下角看到新的View非常小。可可窗口在替换视图时的大小加倍
有没有人知道为什么会发生这种情况?我几个小时都在尝试所有的事情,但我似乎无法找到发生这种情况的原因。
更换视图(以及它的ViewController)我使用下面的代码:
func exchangeDiashowViewController(for newDiashowViewController: NSViewController) {
if let oldDiashowViewControllerAsStateObserver = self.diashowViewController as? DiashowStateObserver {
DiashowManager.sharedInstance.remove(stateObserver: oldDiashowViewControllerAsStateObserver)
}
if let oldDiashowViewControllerAsSpeedObserver = self.diashowViewController as? DiashowSpeedObserver {
DiashowManager.sharedInstance.remove(speedObserver: oldDiashowViewControllerAsSpeedObserver)
}
if let newDiashowViewControllerAsStateObserver = newDiashowViewController as? DiashowStateObserver {
DiashowManager.sharedInstance.add(stateObserver: newDiashowViewControllerAsStateObserver)
}
if let newDiashowViewControllerAsSpeedObserver = newDiashowViewController as? DiashowSpeedObserver {
DiashowManager.sharedInstance.add(speedObserver: newDiashowViewControllerAsSpeedObserver)
}
self.view.replaceSubview(self.diashowViewController.view, with: newDiashowViewController.view)
self.diashowViewController.removeFromParentViewController()
self.addChildViewController(newDiashowViewController)
self.diashowViewController = newDiashowViewController
let centerHorizontallyConstraint = NSLayoutConstraint(item: self.diashowViewController.view, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0)
let centerVerticallyConstraint = NSLayoutConstraint(item: self.diashowViewController.view, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0)
let widthConstraint = NSLayoutConstraint(item: self.diashowViewController.view, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: 0.0)
let heightConstraint = NSLayoutConstraint(item: self.diashowViewController.view, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([centerHorizontallyConstraint, centerVerticallyConstraint, widthConstraint, heightConstraint])
}
我真的希望有人有一个想法。提前感谢!
答
看来self.view.replaceSubview()
是问题所在。
我把一切都用
self.diashowViewController.view.removeFromSuperview()
self.view.addSubview(newDiashowViewController.view)
,而不是工作。