disable elements on UIView behind modal UIView

狂风中的少年 提交于 2019-12-06 09:49:07

问题


i have a UIView that is smaller than the superview so i can represent this view as a modal view when a button is clicked.

I have managed to do the following: * add a subview to the superview. * centered this modal view

I am now trying to make the elements behind the UIView unclickable. And also add a grey shadow te the ourside of my modal view so that the user understands that the modal view is the view in focus.

I would like to know how to achieve this.

I do not wish to use the presentation modal transition. I know and have already implemented this in other projects. Any help is appreciated.


回答1:


The simplest thing would be to lay a fullscreen UIView with a translucent gray background behind your "modal" view. Then it will intercept all of the touches. It might look something like this:

UIView *dimBackgroundView = [[UIView alloc] initWithFrame:theSuperview.bounds];
dimBackgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:.5f];

[theSuperview addSubview:dimBackgroundView];
[theSuperview addSubview:modalView];

For future reference, you can set myView.userInteractionEnabled = NO to disable touch events on a view.




回答2:


There are several ways to do it. If you have a custom view which has custom location, you can modify it like that:

Create an instance var:

UIView* backgroundView;

And whenever you need it, put it behind your custom view:

if (backgroundView == nil)
        backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:.5f];
[self.view addSubview:backgroundView];
[backgroundView animateBump:customView.view];
[backgroundView addSubview:customView.view];

When you do not need it anymore

   [backgroundView removeFromSuperview];


来源:https://stackoverflow.com/questions/4625862/disable-elements-on-uiview-behind-modal-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!