gpt4 book ai didi

ios - 使用 UIPresentationController 和 Tab 栏向上滑动菜单

转载 作者:行者123 更新时间:2023-11-28 15:24:21 25 4
gpt4 key购买 nike

我使用 UIPresentationController 来显示我的向上滑动菜单,我的应用程序中还有标签栏菜单。我的向上滑动菜单显示在我的标签栏上方,但我希望它显示在我的标签栏上方?

所以现在它的行为是这样的:

enter image description here enter image description here

或对框架高度进行一些操作:

enter image description here

但我需要它表现得像这样:

enter image description here

我的代码:

class SlideInPresentationController: UIPresentationController {
private var direction: SlideDirection

init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?,
slideDirection: SlideDirection)
{
direction = slideDirection
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
}

override func containerViewWillLayoutSubviews()
{
presentedView?.frame = frameOfPresentedViewInContainerView
}

override func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize {
switch direction {
case .slideLeft, .slideRight:
return CGSize(width: parentSize.width*(2.0/3.0), height: parentSize.height)
case .slideUp, .slideDown:
return CGSize(width: parentSize.width, height: 88)
}
}

override var frameOfPresentedViewInContainerView: CGRect {
var frame: CGRect = .zero
frame.size = size(forChildContentContainer: presentedViewController, withParentContainerSize: containerView!.bounds.size)
switch direction {
case .slideLeft:
frame.origin.x = containerView!.frame.width - frame.size.width
case .slideUp:
frame.origin.y = containerView!.frame.height - 88-49 //slide up menu + tab bar
default:
frame.origin = .zero
}
return frame
}

}

这里,动画类和SlideInPresentationAnimator的扩展

class SlideInPresentationAnimator: NSObject {
let direction: SlideDirection

enum AnimationType
{
case dismiss
case show
}

let animationType: AnimationType
var mainViewController: UIViewController?

init(type: AnimationType, presentingSlideDirection slideDirection: SlideDirection, controller: UIViewController)
{
direction = slideDirection
animationType = type
mainViewController = controller
super.init()
}

init(type: AnimationType, presentingSlideDirection slideDirection: SlideDirection)
{
direction = slideDirection
animationType = type
super.init()
}
}


extension SlideInPresentationAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
{
return 0.3
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
{
let transitionKey: UITransitionContextViewControllerKey
switch animationType {
case .dismiss:
transitionKey = .from
case .show:
transitionKey = .to
}
let controller = transitionContext.viewController(forKey: transitionKey)!
if animationType == .show
{

transitionContext.containerView.addSubview(controller.view)
}

let inFrame = transitionContext.finalFrame(for: controller)
var outFrame = inFrame
switch direction {
case .slideLeft:
outFrame.origin.x = transitionContext.containerView.frame.size.width
case .slideUp:
outFrame.origin.y = transitionContext.containerView.frame.size.height - 88
default: break
// TODO
}

let startFrame: CGRect, endFrame: CGRect
switch animationType {
case .show:
startFrame = outFrame
endFrame = inFrame
case .dismiss:
startFrame = inFrame
endFrame = outFrame
}
let animationDuration = transitionDuration(using: transitionContext)
controller.view.frame = startFrame
UIView.animate(withDuration: animationDuration, animations: {
controller.view.frame = endFrame
}) { (finished: Bool) in
transitionContext.completeTransition(finished)
}
}
}

最佳答案

在这一行

outFrame.origin.y = transitionContext.containerView.frame.size.height - 88

你还应该减去标签栏的高度

outFrame.origin.y = transitionContext.containerView.frame.size.height - 88 - *height of tab bar*

我相信标签栏的高度曾经是 49,但我已经有一段时间没有使用标签栏了,所以通过使用获取高度可能更好

tabBarController.tabBar.frame.size.height

关于ios - 使用 UIPresentationController 和 Tab 栏向上滑动菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45478402/

25 4 0