Bringing a subview to be in front of all other views

前端 未结 8 1890
走了就别回头了
走了就别回头了 2020-12-07 13:24

I am working on integrating an ad provider into my app currently. I wish to place a fading message in front of the ad when the ad displays but have been completely unsuccess

相关标签:
8条回答
  • 2020-12-07 13:30

    What if the ad provider's view is not added to self.view but to something like [UIApplication sharedApplication].keyWindow?

    Try something like:

    [[UIApplication sharedApplication].keyWindow addSubview:yourSubview]
    

    or

    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:yourSubview]
    
    0 讨论(0)
  • 2020-12-07 13:30

    In Swift 4.2

    UIApplication.shared.keyWindow!.bringSubviewToFront(yourView)
    

    Source: https://developer.apple.com/documentation/uikit/uiview/1622541-bringsubviewtofront#declarations

    0 讨论(0)
  • 2020-12-07 13:32

    Swift 2 version of Jere's answer:

    UIApplication.sharedApplication().keyWindow!.bringSubviewToFront(YourViewHere)
    

    Swift 3:

    UIApplication.shared.keyWindow!.bringSubview(toFront: YourViewHere)
    

    Swift 4:

    UIApplication.shared.keyWindow!.bringSubviewToFront(YourViewHere)
    

    Hope it saves someone 10 seconds! ;)

    0 讨论(0)
  • 2020-12-07 13:34

    try this:

    self.view.layer.zPosition = 1;
    
    0 讨论(0)
  • 2020-12-07 13:34

    Let me make a conclusion. In Swift 5

    You can choose to addSubview to keyWindow, if you add the view in the last. Otherwise, you can bringSubViewToFront.

    let view = UIView()
    UIApplication.shared.keyWindow?.addSubview(view)
    UIApplication.shared.keyWindow?.bringSubviewToFront(view)
    

    You can also set the zPosition. But the drawback is that you can not change the gesture responding order.

    view.layer.zPosition = 1
    
    0 讨论(0)
  • 2020-12-07 13:38

    As far as i experienced zposition is a best way.

    self.view.layer.zPosition = 1;

    0 讨论(0)
提交回复
热议问题