present到新控制器背景色半透明設置

我們開發(fā)中經常會遇到需要彈出一個彈窗且背景色半透明,但是我們設置modalPresentationStyleoverFullScreen背景色是半透明了,但是present時半透明彈窗從設備下面出現(xiàn)時不是漸變?yōu)榘胪该鳎侵苯訌南碌缴弦粕先ィ缦聢D:

Nov-16-2023 23-28-27.gif

下面我們定義要present的控制器的基類BaseSheetViewController

import UIKit

class BaseSheetViewController: UIViewController,UIViewControllerTransitioningDelegate {
    
    init() {
        super.init(nibName:nil,bundle: nil)
        transitioningDelegate = self
        modalPresentationStyle = .custom
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.clear
    }
    
    public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return CustomModayAlphaController(presentedViewController: presented, presenting: presenting)
    }

}

class CustomModayAlphaController: UIPresentationController {
    
    private var CustomFrameOfPresentedInContainerView = CGRect.zero
    private var CustomSetFrameWhenPresentedView = false
    
    override func presentationTransitionDidEnd(_ completed: Bool) {
        super.presentationTransitionDidEnd(completed)
        CustomSetFrameWhenPresentedView = completed
    }
    
    override func containerViewDidLayoutSubviews() {
        super.containerViewDidLayoutSubviews()
        CustomFrameOfPresentedInContainerView = frameOfPresentedViewInContainerView
    }
    
    override var presentedView: UIView? {
        if CustomSetFrameWhenPresentedView {
            super.presentedView?.frame = CustomFrameOfPresentedInContainerView
        }
        return super.presentedView
    }
    
    override func dismissalTransitionWillBegin() {
        super.dismissalTransitionWillBegin()
        CustomSetFrameWhenPresentedView = false
        if let transitionCoordinator = presentingViewController.transitionCoordinator {
            transitionCoordinator.animate(alongsideTransition: { [weak self] _ in
                self?.containerView?.backgroundColor = .clear
            }, completion: nil)
        }
    }
    
    override func presentationTransitionWillBegin() {
        super.presentationTransitionWillBegin()
        containerView?.backgroundColor = .clear
        if let coordinator = presentingViewController.transitionCoordinator {
            coordinator.animate(alongsideTransition: { [weak self] _ in
                self?.containerView?.backgroundColor = UIColor.black.withAlphaComponent(0.4)
            }, completion: nil)
        }
    }
}

假如你要A present B,你可以使B繼承于BaseSheetViewController,直接A.present(B, animated: true),不需要設置modalPresentationStyle,present的效果就達到了B背景色漸變出來了,效果如下:

Nov-16-2023 23-31-26.gif

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容