#ios #swift #uitableview #uiviewcontroller #uitabbar
#iOS #swift #uitableview #uiviewcontroller #uitabbar
Вопрос:
Я программно устанавливаю левое боковое меню на a UITabBarController
. Для этого я вставляю ViewController с индексом 0 в качестве подвида в TabBarController
. Когда я нажимаю значок меню, сначала TabView перемещается вправо, и UITableView
отображается меню, но я не могу взаимодействовать с UITableView
тем, где я ошибся?
Общий 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)
}
}