简体   繁体   English

将子视图置于所有其他视图之前

[英]Bringing a subview to be in front of all other views

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).该函数在广告加载的通知(通知来自广告提供商的 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?无论如何我可以强制我的一个子视图成为最顶层的视图,这样它就不会被任何东西挡住了吗?

试试这个:

self.view.layer.zPosition = 1;

What if the ad provider's view is not added to self.view but to something like [UIApplication sharedApplication].keyWindow ?如果广告提供商的视图没有添加到self.view而是添加到[UIApplication sharedApplication].keyWindow类的东西[UIApplication sharedApplication].keyWindow

Try something like:尝试类似:

[[UIApplication sharedApplication].keyWindow addSubview:yourSubview]

or要么

[[UIApplication sharedApplication].keyWindow bringSubviewToFront:yourSubview]

Swift 2 version of Jere's answer: Jere 回答的 Swift 2 版本:

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

Swift 3:斯威夫特 3:

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

Swift 4:斯威夫特 4:

UIApplication.shared.keyWindow!.bringSubviewToFront(YourViewHere)

Hope it saves someone 10 seconds!希望它能为某人节省 10 秒钟! ;) ;)

I had a need for this once.我有一次需要这个。 I created a custom UIView class - AlwaysOnTopView .我创建了一个自定义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

Have your view extend this class.让你的观点扩展这个类。 Of course this only ensures a subview is above all of its sibling views.当然,这只能确保子视图高于其所有兄弟视图。

As far as i experienced zposition is a best way.就我而言,zposition 是最好的方法。

self.view.layer.zPosition = 1; self.view.layer.zPosition = 1;

In c#, View.BringSubviewToFront(childView);在 C# 中,View.BringSubviewToFront(childView); YourView.Layer.ZPosition = 1; YourView.Layer.ZPosition = 1; both should work.两者都应该工作。

Let me make a conclusion.让我做一个结论。 In Swift 5在斯威夫特 5

You can choose to addSubview to keyWindow, if you add the view in the last.您可以选择将Subview 添加到keyWindow,如果您在最后添加视图。 Otherwise, you can bringSubViewToFront.否则,您可以带来SubViewToFront。

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

You can also set the zPosition.您还可以设置 zPosition。 But the drawback is that you can not change the gesture responding order.但缺点是不能改变手势响应顺序。

view.layer.zPosition = 1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM