简体   繁体   中英

Custom popup over UITableViewController is swipe-able - how to prevent - Swift

I am trying to make a custom pop up over a UITableViewController that is embedded in a UINavigationController but I am experiencing two problems:

  1. The opacity that i determined by designating an alpha value to the background colour of the UIViewcontroller for the pop up appears not to function.
  2. The UIViewcontroller for the popup is swipe-able. If I make a left to right gesture on the screen I am able to push off the pop up. How do I prevent it from behaving like this? I am trying to show a file upload progress so it is important that the pop up is not able to be swiped away.

Please see screen shot below.

    func showProgrssBarPopUp(){

    let popUp = self.storyboard?.instantiateViewController(withIdentifier: "uploadPopUp") as! ProgressBarPopUpViewController
    self.navigationController?.pushViewController(popUp, animated: true)

    }

在此处输入图片说明

The lower viewcontroller content is not viewable, even though alpha value of overlaying popup viewcontroller is set to 0.5:

在此处输入图片说明

The entire viewcontroller for popup is swipe-able:

在此处输入图片说明

在此处输入图片说明

You can set alpha of background

self.view.backgroundColor = UIColor.white.withAlphaComponent(0.2)

You can Present ProgressBar View with modalPresentationStyle as overCurrentContext

 let popUp = self.storyboard?.instantiateViewController(withIdentifier: "uploadPopUp") as! ProgressBarPopUpViewController
        popUp.modalPresentationStyle = .overCurrentContext
self.present(popUp, animated: true, completion: nil)

Just override view controller's appear/dismiss method

class PopUpController: UIViewController {    

override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            if animated {
                view.backgroundColor = .clear
                UIView.animate(withDuration: animationTime) {
                    self.view.layer.backgroundColor = UIColor.black.withAlphaComponent(0.75).cgColor
                }
            }
        }

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    if flag {
        UIView.animate(withDuration: animationTime, animations: {
            self.view.layer.backgroundColor = UIColor.clear.cgColor
        }, completion: { (bool) in
            super.dismiss(animated: false, completion: completion)
        })
    } else {
        super.dismiss(animated: flag, completion: completion)
    }
}
}

Use

let popUp = PopUpController()
popUp.modalPresentationStyle = .overCurrentContext
self.present(popUp, animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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