简体   繁体   English

在变暗的UIWindow上呈现UIView

[英]Present UIView over darkened UIWindow

I am trying to present a UIView while at the same time darkening the rest of the screen. 我试图提供一个UIView,同时使屏幕的其余部分变暗。 However, I am having difficulty. 但是,我遇到了困难。 This is my code: 这是我的代码:

UIWindow *window = [[UIApplication sharedApplication] keyWindow];

self.viewToDarken = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
[self.viewToDarken setBackgroundColor:[UIColor blackColor]];
[self.viewToDarken setAlpha:0.0f];
[window insertSubview:self.viewToDarken aboveSubview:self.hostView];

[self.hostView addSubview:self];


[UIView animateWithDuration:0.25f
                      delay:0.0
                    options:UIViewAnimationCurveLinear
                 animations:^(void) {
                     [self setFrame:[self destinationFrame]];
                     if (self.viewToDarken) self.viewToDarken.alpha = 0.7f;
                 }completion:^(BOOL finished) {
                     [self notifyDidShow];
                 }];

However, I can't get the view to darken to get placed below the view I am adding (self). 但是,我无法使视图变暗,无法置于要添加的视图(自己)的下方。 any ideas why this doesn't work? 任何想法为什么这不起作用?

Thanks! 谢谢!

You are adding the viewToDarken above self.hostView then adding self to hostView so of course self will be below viewToDarken . 要添加的viewToDarken上述self.hostView然后加入selfhostView所以当然自会低于viewToDarken You need to add viewToDarken below self.hostView . 您需要在viewToDarken下面添加self.hostView

What you have now is: 您现在拥有的是:

hostView + self(view) (below) -> viewToDarken hostView + self(view)(下)-> viewToDarken

You need 你需要

viewToDarken (below)-> hostView + self(view) viewToDarken(下)-> hostView + self(视图)

You should have: 你应该有:

[window insertSubview:self.viewToDarken belowSubview:self.hostView];

You have 3 views here; 您在这里有3次浏览; self , hostView and viewToDarken . selfhostViewviewToDarken You are inserting the viewToDarken above hostView , and then you are adding self to the hostView. 您将在viewToDarken上方插入hostView ,然后将self添加到hostView中。 Because self is a subview of hostView , it will be below any views that hostView is below. 由于selfhostView的子视图,因此它将位于hostView所在的任何视图下方。

You probably instead want to add the viewToDarken to hostView . 您可能要改为将viewToDarken添加到hostView Or you could insert it behind the hostView like so: 或者您可以像这样将其插入hostView后面:

[self.hostView.superview insertSubview:self.viewToDarken belowSubview:self.hostView];

Whichever way you do it, you are going to need to take into account coordinate systems. 无论采用哪种方式,都需要考虑坐标系。 Make sure to check out -[UIView convertRect:fromView:] . 确保签出-[UIView convertRect:fromView:]

Also, instead of using absolute "magic" numbers for the frame, use window.bounds: 另外,不要使用绝对的“魔术”数作为框架,而应使用window.bounds:

self.viewToDarken = [[UIView alloc] initWithFrame:window.bounds];

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

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