Swift: UIView.insertSubview( , aboweSubview) 不适用于 iOS 8

Swift: UIView.insertSubview( , aboweSubview) doesn't work on iOS 8

这已经困扰我几个小时了,我似乎找不到解决方案:(。

我有一个 UITableView,当我单击一行时,我想在 "Card" 中显示该行的详细信息。我 在 keywindow 中添加卡片,因为我希望它在 UINavigationBar 之上,并且在 透明视图 的背景中卡片

这就是我的代码的样子

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.vwCard.removeFromSuperview()

    let keyWindow = UIApplication.sharedApplication().keyWindow
    let screen = UIScreen.mainScreen().bounds

    vwTransparentView = UIView(frame: screen)
    vwTransparentView.backgroundColor = UIColor.blackColor()
    vwTransparentView.hidden = true
    vwTransparentView.userInteractionEnabled = false

    keyWindow?.addSubview(self.vwCard)
    keyWindow?.insertSubview(self.vwTransparentView, belowSubview: self.vwCard)


    vwCard.center = CGPointMake((screen.width/2), (screen.height/2))
    vwCard.hidden = true

    vwCard.layer.borderColor = UIColor.whiteColor().CGColor
}

当我点击 UITableView 行时,会发生这种情况

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    vwCard.hidden = false
    vwCard.alpha = 0
    vwTransparentView.alpha = 0
    vwTransparentView.hidden = false
    vwCard.transform = CGAffineTransformMakeScale(1.2, 1.2)
    UIView.animateWithDuration(0.3, animations: { () -> Void in
        self.vwCard.alpha = 1
        self.vwCard.transform = CGAffineTransformIdentity
        self.vwTransparentView.alpha = 0.4
    })

}

这是 iOS 7 (7.1)

中的画面

这就是它在 iOS 8 (8.3)

中的样子

我希望它在 iOS 8 中看起来与在 iOS 7 中一样。我不知道为什么 keyWindow?.insertSubview(self.vwTransparentView, belowSubview: self.vwCard) 不起作用。我也尝试过 insertSubview( ,aboweView:)

显然我必须将代码移动到 viewDidLayoutSubviews()

我还添加了行 vwCard.setTranslatesAutoresizingMaskIntoConstraints(true)

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.vwCard.removeFromSuperview()

    let keyWindow = UIApplication.sharedApplication().keyWindow
    let screen = UIScreen.mainScreen().bounds

    vwTransparentView = UIView(frame: screen)
    vwTransparentView.backgroundColor = UIColor.blackColor()
    vwTransparentView.hidden = true
    vwTransparentView.userInteractionEnabled = false

    keyWindow?.addSubview(self.vwCard)
    keyWindow?.insertSubview(self.vwTransparentView, belowSubview: self.vwCard)


    vwCard.center = CGPointMake((screen.width/2), (screen.height/2))
    vwCard.hidden = true
    vwCard.setTranslatesAutoresizingMaskIntoConstraints(true)

    vwCard.layer.borderColor = UIColor.whiteColor().CGColor
}