Bringing a subview to be in front of all other views

前端 未结 8 1892
走了就别回头了
走了就别回头了 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:38

    In c#, View.BringSubviewToFront(childView); YourView.Layer.ZPosition = 1; both should work.

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

    I had a need for this once. I created a custom UIView class - AlwaysOnTopView.

    @interface AlwaysOnTopView : UIView
    @end
    
    @implementation AlwaysOnTopView
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if (object == self.superview && [keyPath isEqual:@"subviews.@count"]) {
            [self.superview bringSubviewToFront:self];
        }
    
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
    
    - (void)willMoveToSuperview:(UIView *)newSuperview {
        if (self.superview) {
            [self.superview removeObserver:self forKeyPath:@"subviews.@count"];
        }
    
        [super willMoveToSuperview:newSuperview];
    }
    
    - (void)didMoveToSuperview {
        [super didMoveToSuperview];
    
        if (self.superview) {
            [self.superview addObserver:self forKeyPath:@"subviews.@count" options:0 context:nil];
        }
    }
    
    @end
    

    Have your view extend this class. Of course this only ensures a subview is above all of its sibling views.

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