Home How to pop-over a UIViewController
Reply: 2

How to pop-over a UIViewController

AnonProgrammer
1#
AnonProgrammer Published in 2017-12-07 21:27:50Z

I am trying to get a UIViewController transition effect where ViewControllerB pops over ViewContorllerA- while still revealing ViewcontrllerA in the background. Any idea how I can accomplish this?

Transition:

Pak Ho Cheung
2#
Pak Ho Cheung Reply to 2017-12-07 23:33:35Z

!1. create a uiview

@IBOutlet weak var viewTemp: UIView!

!2. positioning

let theScreenWidth = self.view.frame.size.width
let theScreenHeight = self.view.frame.height
viewTemp.frame = CGRect(x: 0, y: theScreenHeight+64, width: theScreenWidth, height: theScreenHeight-64)

!3. use animate to open

UIView.animate(withDuration: 0.5, delay: 0, options: .curveLinear, animations: {
    viewTemp.transform = CGAffineTransform(translationX: 0, y: -self.view.frame.height)
}) { (success: Bool) in
}

!4. use animate to close

UIView.animate(withDuration: 0.5, delay: 0, options: .curveLinear, animations: {
    viewTemp.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height)
}) { (success: Bool) in
}
Josh O'Connor
3#
Josh O'Connor Reply to 2017-12-08 03:01:30Z

Try this. Create a view controller on a storyboard, set the class to CustomDialog. Add view to this view controller, which will be what you want in your popup. When you want the dialog to lose, call confirmBlock() or cancelBlock().

//CustomDialog class

import Foundation

class CustomDialog: UIViewController, UIGestureRecognizerDelegate {

    var confirmBlock: ((_ result: AnyObject) -> ())?
    var cancelBlock: (() -> ())?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        definesPresentationContext = true
        providesPresentationContextTransitionStyle = true
        modalPresentationStyle = .overFullScreen
        modalTransitionStyle = .crossDissolve
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        makeBlurBackground()
        let tapDismiss = UITapGestureRecognizer(target: self, action: #selector(BaseCustomDialog.tapOutside))
        tapDismiss.delegate = self
        view.addGestureRecognizer(tapDismiss)
        view.tag = GestureRecognizerUniqueTag
    }

    @objc fileprivate func tapOutside() {
        cancelAction()
    }

    func makeBlurBackground() {
        let effectView = UIView(frame: self.frame)
        effectView.backgroundColor = ClickUpConstants.defaultTransparentBackgroundColor
        effectView.isUserInteractionEnabled = false

        view.addSubview(effectView)
        view.sendSubview(toBack: effectView)

    }

    func confirmAction(_ result: AnyObject) {
    confirmBlock?(result)
        cleanupAndDismiss()
    }

    func cancelAction() {
        cancelBlock?()
        cleanupAndDismiss()
    }

    fileprivate func cleanupAndDismiss() {
        self.view.endEditing(true)

        dismiss(animated: true) { () -> Void in
            self.confirmBlock = nil
            self.cancelBlock = nil
        }
    }
}

How to call dialog (i.e: when button is pressed)

let dialog = UIStoryboard(name: "CustomDialogs", bundle: nil).instantiateViewController(withIdentifier: customDialog") as! CustomDialog
let navigationController = UINavigationController(rootViewController: dialog)
navigationController.isNavigationBarHidden = true


dialog.cancelBlock = {
    //do something when cancel
    dialog.dismiss(animated: true, completion: nil)
}

dialog.confirmBlock = {
//do something when cancel
    dialog.dismiss(animated: true, completion: nil)
}

self.present(navigationController, animated: true, completion: nil)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303353 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO