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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13182482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:03:59  来源:igfitidea点击:

Bringing a subview to be in front of all other views

iosuiviewsubview

提问by Mark S

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.

该函数在广告加载的通知(通知来自广告提供商的 SDK)时触发。但是,我的子视图并没有出现在广告前面。我想这是广告提供商故意使这变得困难的,但无论如何我都希望这样做。我目前正在与提供商讨论是否允许这样做。但就目前而言,我只是想看看这是否可能。

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?

无论如何我可以强制我的一个子视图成为最顶层的视图,这样它就不会被任何东西挡住了吗?

回答by daniel kilinskas

try this:

尝试这个:

self.view.layer.zPosition = 1;

回答by jere

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

如果广告提供商的视图没有添加到self.view诸如[UIApplication sharedApplication].keyWindow?

Try something like:

尝试类似:

[[UIApplication sharedApplication].keyWindow addSubview:yourSubview]

or

或者

[[UIApplication sharedApplication].keyWindow bringSubviewToFront:yourSubview]

回答by Paul Lehn

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 秒钟!;)

回答by rmaddy

I had a need for this once. I created a custom UIViewclass - 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.

让你的观点扩展这个类。当然,这只能确保子视图高于其所有兄弟视图。

回答by Gobi M

As far as i experienced zposition is a best way.

就我而言,zposition 是最好的方法。

self.view.layer.zPosition = 1;

self.view.layer.zPosition = 1;

回答by Anisetti Nagendra

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

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

回答by Zgpeace

Let me make a conclusion. In Swift 5

让我做一个结论。在斯威夫特 5

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

您可以选择将Subview 添加到keyWindow,如果您在最后添加视图。否则,您可以带来SubViewToFront。

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.

您还可以设置 zPosition。但缺点是不能改变手势响应顺序。

view.layer.zPosition = 1

回答by Vinoth Vino

回答by user5266363

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

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