Hide UITableView on touches outside the tableview

前端 未结 3 1392
心在旅途
心在旅途 2021-01-25 00:13

I have a small UITableView that is hidden when the view is loaded. When i click on \"SHOW\" UIButton, the UITableView<

3条回答
  •  长发绾君心
    2021-01-25 00:37

    Best Approach

    Simple.Before show up the UITable View add one more grayed out/Transparent view then add tap gesture recogniser on it to hide it . That's it.

    1. Show Overlay View first - alpha will be 0.5f and background color should be clear color.

    2. show the table view.

    NOTE: over lay view should have tap recogniser which will hide the overlay and table view

    in View did load

    UITapGestureRecognizer *tapRecog = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                    action:@selector(overlayDidTap:)];
    
    [myOverLayView addGestureRecognizer:tapRecog];
    
    
    
    - (void)overlayDidTap:(UITapGestureRecognizer *)gesture
    {
    
        //hide both overlay and table view here
    
    }
    

    Bad Approach

    We should not add tap recogniser on main view itself. Because it may have lots of controls inside of it. so when user tap on it. it will perform its operation. So to avoid it we can simulate the same behaviour by above approach

提交回复
热议问题