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

2.6k views Asked by At

This have been bugging me for some hours now, and i can't seem to find a solution :(.

I have a UITableView, and when i click on a row i want to show a detailed information for that row in a "Card". I add the card in the keywindow because i want it to be above the UINavigationBar, and have a transparent view in the background of the card

This is how my code looks like

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
}

When I click on UITableView row this happens

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
    })

}

This is how the screen looks like in iOS 7 (7.1)

enter image description here

And this is how it looks in iOS 8 (8.3)

enter image description here

I want it to look the same way in iOS 8 as it does in iOS 7. I dont know why keyWindow?.insertSubview(self.vwTransparentView, belowSubview: self.vwCard) is not working. I have also tried it the other way around with insertSubview( ,aboweView:)

1

There are 1 answers

0
Ibrahim Yildirim On BEST ANSWER

Apparently what i had to was to move the code to viewDidLayoutSubviews()

Also i added the line 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
}