首页 > 解决方案 > 如何以编程方式在 swift 5 (Xcode) 中获取呈现视图的大小

问题描述

如何在 swift 5中以编程方式获取呈现的视图页面表的大小。

在此处输入图像描述

一世

标签: swift

解决方案


试试这个。

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

推荐阅读