swift - 如何以编程方式在 swift 5 (Xcode) 中获取呈现视图的大小
解决方案
试试这个。
ViewController.swift
import UIKit
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
@IBAction func gotoVCB(_ sender: UIButton) {
let vc = DetailsViewController()
vc.modalPresentationStyle = .custom
present(vc, animated: true, completion: nil)
}
}
DetailsViewController.swift
import UIKit
class DetailsViewController: UIViewController {
var backgroundView = UIView()
let storeBack = UIView()
let storeHeight = UIScreen.main.bounds.height / 2
var isPop = false
required init?(coder aDecoder: NSCoder) {
fatalError("init has not been implemented")
}
init() {
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .custom
transitioningDelegate = self
}
fileprivate func UI() {
view.backgroundColor = .clear
view.addSubview(backgroundView)
backgroundView.frame = self.view.bounds
backgroundView.backgroundColor = UIColor.black
backgroundView.alpha = 0.8
view.addSubview(storeBack)
storeBack.backgroundColor = .yellow
storeBack.translatesAutoresizingMaskIntoConstraints = false
storeBack.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
storeBack.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
storeBack.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
storeBack.heightAnchor.constraint(equalToConstant: storeHeight).isActive = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleEvent(_:)))
backgroundView.addGestureRecognizer(tapGesture)
}
override func viewDidLoad() {
super.viewDidLoad()
UI()
}
@objc func handleEvent(_ sender: UITapGestureRecognizer) {
dismiss(animated: true, completion: nil)
}
}
extension DetailsViewController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
print(String(describing: type(of: presented)))
print(String(describing: type(of: presenting)))
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 1
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let back = transitionContext.containerView
let overView = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
guard let vc = overView else { return }
isPop = !isPop
if isPop == true {
back.addSubview(vc.view)
storeBack.frame.origin.y += storeHeight
backgroundView.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseOut], animations: {
self.storeBack.frame.origin.y -= self.storeHeight
self.backgroundView.alpha = 1
}, completion: { (finished) in
transitionContext.completeTransition(true)
})
} else {
UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseOut], animations: {
self.storeBack.frame.origin.y += self.storeHeight
self.backgroundView.alpha = 0
}, completion: { (finished) in
transitionContext.completeTransition(true)
})
}
}
}
推荐阅读
- sql-server - 如何使用分组查询在 SQL Server 中创建表
- wordpress - Wordpress 无法在 ubuntu 18.04 上运行,http://xxxxxxxxxx/wp-admin/install.php 没有给出对象 404 错误
- sql - 如果行数据相同,则将数字加 1
- php - laravel 文件上传到文件夹和数据库
- android - 如何处理巨大的 JSON 数组?
- flutter - 如何滚动到包含 Flutter 中展开项目的列中的 TextView?
- python - Python 3 TypeError:并非所有参数都在字符串格式化期间转换,列出问题
- objective-c - Xcode 10 错误 PrefixHeader.pch.gch' 不是有效的预编译 PCH 文件
- internals - Reasonml 地图不纯?
- php - 使用codeigniter框架时如何在cpanel中安装composer?