调用removeFromSuperView()后,SubView(nib)不会删除

时间:2017-08-15 18:08:27

标签: ios swift xcode

我有一个覆盖视图来隔离内容,我在viewWillAppear()中检查身份验证,并且我有一个Notification订阅了我的Auth方法。如果我在显示任何其他视图之前进行身份验证,则覆盖图不会显示,但是它会在第一个视图上显示,即使在调用removeFromSuperView()之后也不会消失。

&&

1 个答案:

答案 0 :(得分:1)

这是因为您每次都试图删除新的ForceSignInBanner。理想情况下,您应该创建一次并保留对ForceSignInBanner创建的引用(作为ProtectedViewController的可选属性)。

然后移除您已存储在该媒体资源中的ForceSignInBanner

    class ProtectedViewController: UIViewController, ForceSignInBannerDelegate {

        // This lazily loads the view when the property is first used and sets the delegate.
        // Ideally you wouldn't force-case the `as` but I've left it for simplicity here.

        private lazy var forceSignInBannerView: ForceSignInBanner = {
            let forceSignInBannerView  = ForceSignInBanner.instanceFromNib() as! ForceSignInBanner
            forceSignInBannerView.delegate = self
            return forceSignInBannerView
        }()

        // ... your other code ... //

        fun toggleForceSignInBannerViewVisibility(isVisible: Bool) {
            if isVisible {
                view.addSubview(forceSignInBannerView)
            } else {
                forceSignInBannerView.removeFromSuperview()
            }
        }

    }