您好,登錄后才能下訂單哦!
要使用UIPresentationController來(lái)定制模態(tài)視圖的呈現(xiàn)方式,首先需要?jiǎng)?chuàng)建一個(gè)自定義的PresentationController類,繼承自UIPresentationController。在這個(gè)類中,可以重寫一些方法來(lái)自定義模態(tài)視圖的呈現(xiàn)效果。
下面是一個(gè)簡(jiǎn)單的示例代碼,演示了如何使用UIPresentationController來(lái)定制模態(tài)視圖的呈現(xiàn)方式:
import UIKit
class CustomPresentationController: UIPresentationController {
override var shouldRemovePresentersView: Bool {
return true // 是否移除背景視圖
}
override func presentationTransitionWillBegin() {
// 在模態(tài)視圖呈現(xiàn)之前執(zhí)行的操作
let dimmingView = UIView()
dimmingView.frame = containerView!.bounds
dimmingView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
dimmingView.alpha = 0.0
containerView?.insertSubview(dimmingView, at: 0)
presentedViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
dimmingView.alpha = 1.0
}, completion: nil)
}
override func dismissalTransitionWillBegin() {
// 在模態(tài)視圖消失之前執(zhí)行的操作
containerView?.subviews.first?.alpha = 0.0
}
override func containerViewWillLayoutSubviews() {
// 設(shè)置模態(tài)視圖的frame
presentedView?.frame = frameOfPresentedViewInContainerView
}
override func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize {
return CGSize(width: parentSize.width, height: parentSize.height * 0.5) // 設(shè)置模態(tài)視圖的大小
}
override var frameOfPresentedViewInContainerView: CGRect {
guard let containerView = containerView else { return .zero }
let presentedViewSize = size(forChildContentContainer: presentedViewController, withParentContainerSize: containerView.bounds.size)
return CGRect(x: 0, y: containerView.bounds.height * 0.5, width: presentedViewSize.width, height: presentedViewSize.height)
}
}
在創(chuàng)建自定義PresentationController類后,需要在呈現(xiàn)模態(tài)視圖的時(shí)候指定這個(gè)PresentationController類,例如:
let presentedViewController = YourViewController()
presentedViewController.modalPresentationStyle = .custom
presentedViewController.transitioningDelegate = self
presentedViewController.presentationController?.delegate = self
self.present(presentedViewController, animated: true, completion: nil)
需要注意的是,在使用自定義PresentationController時(shí),需要設(shè)置模態(tài)視圖的modalPresentationStyle為.custom,并且設(shè)置transitioningDelegate為自定義PresentationController。同時(shí),也可以設(shè)置delegate為自定義PresentationController來(lái)進(jìn)一步定制模態(tài)視圖的呈現(xiàn)方式。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。