如何将模态弹出窗口大小更改为等于其父视图大小?
How to change Modal Popup size to equal to its superview size?
我在情节提要中创建了一个模态弹出窗口,因此当单击图像时会出现该图像的弹出窗口,但可以调整大小。我想要的只是弹出窗口等于超级视图的大小(以编程方式)。
如果需要,我会提供额外的代码。
@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()
}
}
}
如有任何帮助,我们将不胜感激。
终于找到答案了。
而不是:
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)
所有这些都解决了问题。
我在情节提要中创建了一个模态弹出窗口,因此当单击图像时会出现该图像的弹出窗口,但可以调整大小。我想要的只是弹出窗口等于超级视图的大小(以编程方式)。
如果需要,我会提供额外的代码。
@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()
}
}
}
如有任何帮助,我们将不胜感激。
终于找到答案了。
而不是:
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)
所有这些都解决了问题。