disable elements on UIView behind modal UIView

2020-07-24 06:56发布

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.

2条回答
别忘想泡老子
2楼-- · 2020-07-24 07:15

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.

查看更多
干净又极端
3楼-- · 2020-07-24 07:17

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];
查看更多
登录 后发表回答