gpt4 book ai didi

ios - 为什么 UITableView 没有获得任何用户交互,因为它的 ViewController 作为 subView 添加到 UITabBarController

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

我正在以编程方式将左侧菜单设置为 UITabBarController。为此,我将索引为 0 的 viewController 作为 subview 插入 TabBarController。当我首先按下菜单图标时,tabView 向右移动并显示菜单 UITableView 但我无法与 UITableView 交互,我哪里做错了吗?

enter image description here

UITabBarController.swift 文件

import UIKit

class MainTabBarController: UITabBarController {

// MARK: Properties

var menuController: MenuController!
var centerController: UIViewController!
var isExpanded = false

// MARK: Initialization

override func viewDidLoad() {
super.viewDidLoad()
configureHomeController()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}

override var prefersStatusBarHidden: Bool {
return isExpanded
}

// MARK: Handlers

func configureHomeController() {
let navigationController = self.viewControllers![0] as! UINavigationController
let homeController = navigationController.viewControllers[0] as! FirstViewController
homeController.homeControllerDelegate = self
centerController = homeController
}

//Configuring Side Menu Controller

func configureMenuController() {
if menuController == nil {
// add menu controller here
menuController = MenuController()
menuController.homeControllerDelegate = self
// Add Child View Controller
addChild(menuController)
// Add Child View as Subview
view.insertSubview(menuController.view, at: 0)
// Configure Child View
menuController.view.frame = view.bounds
menuController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Notify Child View Controller
menuController.didMove(toParent: self)
}
}

func animateSideMenuOpeningAndClosing(shouldExpand: Bool, menuItem: MenuItem?) {
if shouldExpand {
// Show Menu
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
self.centerController.view.frame.origin.x = self.centerController.view.frame.width - 80
}, completion: nil)
} else {
// Hide Menu
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0,options: .curveEaseInOut, animations: {
self.centerController.view.frame.origin.x = 0
}) { (_) in
guard let menuItem = menuItem else {return}
self.didSelectMenuItem(menuItem: menuItem)
}
}
animateStatusBar ()
}

func didSelectMenuItem(menuItem: MenuItem) {
switch menuItem {
case .Dashboard:
print("Show Dashboard")
case .Profile:
print("Show Profile")
case .Notifications:
print("Show Notifications")
case .Contacts:
let controller = SettingsController()
controller.username = "YAMIN"
present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
}
}

func animateStatusBar () {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0,options: .curveEaseInOut, animations: {
self.setNeedsStatusBarAppearanceUpdate()
}, completion: nil)
}

}

extension MainTabBarController: HomeControllerDelegate {
func handleMenuToogle(forMenuItem menuItem: MenuItem?) {
print("Pressed")
if !isExpanded {
configureMenuController()
}
isExpanded = !isExpanded
animateSideMenuOpeningAndClosing(shouldExpand: isExpanded, menuItem: menuItem)
}

}

最佳答案

您在 0 索引处插入 subview ,因此,我认为有些 View 在 subview 层次结构中更高。

您可以使用“调试 View 层次结构按钮”查看您的 subview enter image description here

关于ios - 为什么 UITableView 没有获得任何用户交互,因为它的 ViewController 作为 subView 添加到 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55605477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com