如何将模态弹出窗口大小更改为等于其超视图大小?

时间:2017-05-08 19:15:00

标签: ios swift xcode storyboard modalpopup

我在故事板中创建了一个模态弹出窗口,这样当单击图像时会弹出该图像,但可以调整大小。我想要的只是弹出窗口等于superview的大小(以编程方式)。

如果需要,我会提供额外的代码。

@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()
    }

}

}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

终于找到了答案。

而不是:

imageView.frame = self.view.frame

我只需要使用:

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

在animateOut()块中,我将变换比例更改为1

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

解决了这个问题。