带来了子视图是在所有其他意见前(Bringing a subview to be in front

2019-07-03 12:40发布

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 unsuccessful.

I made a function which adds a subview to my current view and tries to bring it to the front using

[self.view bringSubviewToFront:mySubview]

The function fires on notification that the ad loaded (the notification is from the adprovider's SDK). However, my subview does not end up in front of the ad. I imagine this is made purposely difficult by the ad provider, but I wish to do it regardless. I am currently discussing with the provider whether or not this can be allowed. But for the time being, I just want to see if it's even possible.

Is there anyway I can force a subview of mine to be the top-most view such that it will not be obstructed by anything?

Answer 1:

如果什么广告提供商的观点是不添加到self.view但像[UIApplication sharedApplication].keyWindow

尝试是这样的:

[[UIApplication sharedApplication].keyWindow addSubview:yourSubview]

要么

[[UIApplication sharedApplication].keyWindow bringSubviewToFront:yourSubview]


Answer 2:

试试这个:

self.view.layer.zPosition = 1;


Answer 3:

雨燕2.0版本的Jere的回答:

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

斯威夫特3:

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

斯威夫特4:

UIApplication.shared.keyWindow!.bringSubviewToFront(YourViewHere)

希望这样可以节省别人10秒! ;)



Answer 4:

我有一个需要进行一次。 我创建了一个定制UIView类- 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

有您的视图扩展此类。 当然,这不仅保证子视图首先是它的兄弟姐妹的意见。



Answer 5:

至于我经历z位置是最好的办法。

self.view.layer.zPosition = 1;



Answer 6:

雨燕4.2

UIApplication.shared.keyWindow!.bringSubviewToFront(yourView)

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



Answer 7:

xamarin IOS:this.InsertSubview(subview_youwant_beback,0);



文章来源: Bringing a subview to be in front of all other views