![](/img/trans.png)
[英]Accessing a ViewController's subview that's added on the storyboard
[英]Why UITableView is not getting any user interaction as it's ViewController is added to a UITabBarController as subView
我以編程方式將左側菜單設置為UITabBarController
。 為此,我要插入一個帶有0索引的viewController作為TabBarController
subView。 當我按下菜單圖標時,第一個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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.