iosswiftxcodestoryboardmodalpopup

How to change Modal Popup size to equal to its superview size?


I created a modal popup in storyboard, so that when an image is clicked there is a popup of that image, but resizable. All I want is so that the popup is equal to the size of the superview (programmatically).

I will provide additional code if needed.

@IBOutlet var imageView: UIView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var zoomImageView: UIImageView!
@IBOutlet weak var backgroundButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.setNavigationBarHidden(true, animated: false)

    imageView.layer.cornerRadius = 10
    imageView.layer.masksToBounds = true

    self.scrollView.minimumZoomScale = 1.0
    self.scrollView.maximumZoomScale = 6.0

}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {

    return self.zoomImageView

}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    if UIDevice.current.orientation.isLandscape{
        imageView.center = self.view.center
        imageView.frame = view.frame

    } else if UIDevice.current.orientation.isPortrait{
        imageView.center = self.view.center
        imageView.frame = view.frame
    }
}

@IBAction func showImageView(_ sender: Any) {

    animateIn()

}

@IBAction func closeImageView(_ sender: Any) {

    animateOut()

}

func animateIn() {

    self.scrollView.zoomScale = 1.0

    self.view.addSubview(imageView)
    imageView.center = self.view.center

    imageView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
    imageView.alpha = 0

    self.backgroundButton.alpha = 0.7

    UIView.animate(withDuration: 0.4) {
        self.imageView.alpha = 1
        self.imageView.transform = CGAffineTransform.identity
    }

}

func animateOut() {

    UIView.animate(withDuration: 0.3, animations: {
        self.imageView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
        self.imageView.alpha = 0

        self.backgroundButton.alpha = 0

    }) { (success:Bool) in
        self.imageView.removeFromSuperview()
    }

}

}

Any help would be greatly appreciated.


Solution

  • Finally found the answer.

    Instead of:

    imageView.frame = self.view.frame
    

    I just had to use:

    imageView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
    

    And in the animateOut() block I changed the transform scale to 1

    self.image.transform = CGAffineTransform(scaleX: 1, y: 1)
    

    All that fixed the problem.