###前提・実現したいこと
SlideMenuControllerSwiftの使い方とカスタマイズ - Qiitaを参考にスライドメニューを作ろうと試みました。
スライドメニューの実装先は、Top画面からButtonでクリックした先の画面です。
画面A→画面(ここにスライドメニューを導入)
前提としてTop画面にTabBarControllerを、そして画面AとBにはNavigationControllerと導入しています。StoryboardEntryPointはSlideVC(SlideMenuController)に設定しています。
配置は、TimetableVC(MainViewController)に配置しています。
###発生している問題・エラーメッセージ
結果としてrightMenuViewControllerを実装しスライドメニューを表示することはできました。
しかし、TimetableVC(MainViewController、StoryboardID:Main)上の操作(UICollectionViewがクリック)ができなくなりました。
試しにStoryboardEntryPointをTimetableVC(MainViewController)に設定すると、操作は出来るようになるがスライドが出てこない状態となりました。
###該当のソースコード
swift
1import UIKit 2import SlideMenuControllerSwift 3 4class TimetableVC: UIViewController { 5 6 @IBOutlet weak var timetableCV: UICollectionView! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 //NavigationBarが半透明かどうか 11 navigationController?.navigationBar.isTranslucent = false 12 13 } 14 15 override func didReceiveMemoryWarning() { 16 super.didReceiveMemoryWarning() 17 // Dispose of any resources that can be recreated. 18 } 19 20} 21 22extension TimetableVC: UICollectionViewDataSource { 23 func numberOfSections(in collectionView: UICollectionView) -> Int { 24 // section数は1つ 25 return 1 26 } 27 28 func collectionView(_ collectionView: UICollectionView, 29 numberOfItemsInSection section: Int) -> Int { 30 // 要素数を入れる、要素以上の数字を入れると表示でエラーとなる 31 return 144; 32 } 33 34 func collectionView(_ collectionView: UICollectionView, 35 cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 36 37 // "Cell" はストーリーボードで設定したセルのID 38 let timetableCell:UICollectionViewCell = 39 collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", 40 for: indexPath) 41 42 return timetableCell 43 } 44} 45 46extension TimetableVC: UICollectionViewDelegate { 47 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 48 if let cell = timetableCV.cellForItem(at: indexPath) { 49 cell.tag += 1 50 // 既に選択したセルに対しての処理 51 if cell.tag % 2 == 0 { 52 cell.isSelected = false 53 } 54 // 色を付けるかどうか 55 if cell.isSelected == true { 56 cell.backgroundColor = UIColor.red 57 } else if cell.isSelected == false { 58 cell.backgroundColor = UIColor.clear 59 } 60 } 61 } 62} 63 64extension TimetableVC: UICollectionViewDelegateFlowLayout { 65 // アイテムの大きさを設定 66 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 67 let width = self.view.frame.width / 6 - 16 68 let height = self.view.frame.height / 24 - 10 69 return CGSize(width: width, height: height) 70 } 71 72 // Cell上下の空白 73 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 74 return 0 75 } 76}
SlideVC.swift
Swift
1import UIKit 2import SlideMenuControllerSwift 3 4class SlideVC: SlideMenuController { 5 6 override func awakeFromNib() { 7 let timetableVC = storyboard?.instantiateViewController(withIdentifier: "Main") 8 let rightVC = storyboard?.instantiateViewController(withIdentifier: "Right") 9 10 //ライブラリ特有のプロパティにセット 11 mainViewController = timetableVC 12 rightViewController = rightVC 13 14 super.awakeFromNib() 15 } 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 } 20 21}
swift
1import UIKit 2 3class RightVC: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view. 8 } 9}
###試したこと
SlideVCを生成せずAppDelegate.swiftに以下のようなコードを書いたパターンも行ってみたのですが、結果は変わりませんでした。
swift
1 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 2 // Override point for customization after application launch. 3 let storyboard = UIStoryboard(name: "Timetable", bundle: nil) 4 let timetableVC = storyboard.instantiateViewController(withIdentifier: "Main") 5 let rightVC = storyboard.instantiateViewController(withIdentifier: "Right") 6// let navigationController = UINavigationController(rootViewController: mainVC) 7 let slideMenuController = SlideMenuController(mainViewController:timetableVC, rightMenuViewController: rightVC) 8 slideMenuController.automaticallyAdjustsScrollViewInsets = true 9 slideMenuController.delegate = timetableVC as? SlideMenuControllerDelegate 10 self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0) 11 self.window?.makeKeyAndVisible() 12 13 return true 14 }
この問題に関して教えていただければ幸いです。よろしくお願いいたします。
###補足情報(言語/FW/ツール等のバージョンなど)
swift 4.0 / Xcode 9.0.1 を使用しています。
回答1件
あなたの回答
tips
プレビュー