是否有可能暗淡UIView,但允许背后的元素触摸?

你怎么会在顶部有一个灰色的覆盖,但允许触摸它下面的button?

您可以将overlay的userInteraction设置为false

如上所述,您可以将userInteractionEnabled设置为NO

但要注意:当你创build一个全屏透明视图,并将其userInteractionEnabled设置为NO该视图的所有子视图将不会响应用户操作 。 如果你在视图上添加一个button,它将不会响应用户点击! 还有另外一个问题:如果你设置该视图的alpha值是透明的,例如0.4 ,那么它的所有子视图也将是透明的!

解决方法很简单:不要在透明视图中放置任何子视图,而是将其他元素添加为视图的同胞。 这里是Objective-C代码来展示我的意思:(注意说明这一切的意见):

 //Create a full screen transparent view: UIView *vw = [[UIView alloc] initWithFrame:self.view.bounds]; vw.backgroundColor = [UIColor greenColor]; vw.alpha = 0.4; vw.userInteractionEnabled = NO; //Create a button to appear on top of the transparent view: UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 80, 44)]; btn.backgroundColor = [UIColor redColor]; [btn setTitle:@"Test" forState:UIControlStateNormal]; [btn setTitle:@"Pressed" forState:UIControlStateHighlighted]; //(*) Bad idea: [vw addSubview:btn]; //(**) Correct way to have the button respond to user interaction: // Add it as a sibling of the full screen view [self.view addSubview:vw]; [self.view addSubview:btn];