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

标签 ios swift

我使用 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/

相关文章:

ios - UIPageViewController 未在单击按钮时完成转换

iphone - 这种在 Xcode 4 中重新引用另一个项目的方法有什么问题?

ios - 使用 JSON 进行基本身份验证

ios - 应用程序被电话 iOS 打断后音频无法播放

iPhone iOS 如何更改 UITextView 中某些单词的颜色?

iOS 键盘 - 在应用程序级别以编程方式设置 keyboardAppearance 等属性

swift - UITabBar 在 "Back"按钮后消失

ios - 在 UIViewControllers 中传递 NSManagedObjects 的方法

ios - 如何在SpriteKit中创建场景切换的无限滚动背景?

ios - 如何检测 iOS 中的 sim 更改