ios - viewController.view 不显示,但在 ViewDebugMode 时出现

标签 ios swift uikit

我正在使用 PanGestureaddChild 创建幻灯片菜单。

虽然 ParentViewController 可以向左滑动,但是放在它右边的 MenuViewController 没有显示。

但是,当您使用 ViewDebug 检查时,它确实存在并且放置在预期的位置。

但是为什么它没有显示在实际屏幕上呢?

👇 当 View 被平移时 enter image description here

👇 当查看调试并点击此按钮时 enter image description here enter image description here

class ParentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panGesturePanned)))

        let menuViewController = MenuViewController()
        menuViewController.view.frame = CGRect(
            x: view.frame.maxX,
            y: 0,
            width: view.bounds.width,
            height: UIScreen.main.bounds.height)
        view.addSubview(menuViewController.view)
        addChild(menuViewController)
        menuViewController.didMove(toParent: self)
    }

    @objc private func panGesturePanned(gesture: UIPanGestureRecognizer) {
        guard let gestureView = gesture.view else { return }

        switch gesture.state {
        case .began:
            print()
        case .changed:
            tabBarController?.view.frame.origin.x = min(max(gesture.translation(in: gestureView).x, -240), 0)

        case .cancelled:
            print()

        case .ended:
            print()

        default: break
        }
    }
}

最佳答案

您正在移动 tabBarController,但据我了解您想要移动 menuViewController。试试这个:

@objc private func panGesturePanned(gesture: UIPanGestureRecognizer) {
        guard let gestureView = gesture.view else { return }

        switch gesture.state {
        case .began: break
        case .changed:
            tabBarController?.view.frame.origin.x = max(-menuViewController.view.bounds.width, gesture.translation(in: gestureView).x)
            menuViewController.view.frame.origin.x = max(0, menuViewController.view.bounds.width - gesture.translation(in: gestureView).x)
        case .cancelled: break
        case .ended: break
        default: break
        }
    }

关于ios - viewController.view 不显示,但在 ViewDebugMode 时出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755722/

相关文章:

ios - 如何从 Web Link 将 .ipa 文件安装到 iPhone?

ios - 去另一个 View Controller 后恢复音乐

iphone - UIAppearance 代理缓存编译时间之间的外观?

ios - 如何使用 MessageKit 创建自定义单元格?

ios - 匹配 'transparent' 导航栏的颜色

ios - 调整 UISearchController 搜索栏的 insets

ios - UITableViewCell 布局在重用单元格之前不会更新

ios - x264 编码使用 x264_picture_clean 崩溃

javascript - Mobile safari : frame. src 与 window.location

ios - Swift - 导入 Objective-C 给出 "Unknown type name"