実現したいこと
XLPagerTabStripというライブラリを使って上タブを実装しているのですが、今回下タブも追加したいと思いUITabBarControllerの上にXLPagerTabStripで作った上タブをのせたいと考えています
困っていること
TabBarに追加する時のinitialize引数でNSCoderを渡す必要があるのですがこれにいったい何を渡せばいいのかわからず困っています。
TabBarのコードは以下です。Rootで開くVCをSceneDelegateでTabViewControllerにしているのでrequired initは通らずに普通のinitで初期化されてしまう為NSCoderをinit時に取得することができません。
どうしたらいいのでしょうか?
swift
1class TabViewController: UITabBarController { 2 3 init(){ 4 super.init(nibName: String(describing: TabViewController.self), bundle: nil) 5 } 6 7 required init?(coder aDecoder: NSCoder) { 8 super.init(coder: aDecoder) 9 } 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 var viewControllers: [UIViewController] = [] 15 let firstViewController = UINavigationController(rootViewController: UpperTabListBaseViewController(coder: // ここにい何を入れていいのかわからない)) 16 firstViewController.tabBarItem = UITabBarItem(title: "ホーム", image: #imageLiteral(resourceName: "home"), tag: 1) 17 viewControllers.append(firstViewController) 18 } 19} 20
追記
UpperTabListBaseViewControllerのコードです
class UpperTabListBaseViewController: BaseButtonBarPagerTabStripViewController<UpperTabStockDayCollectionViewCell> { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) buttonBarItemSpec = ButtonBarItemSpec.nibFile(nibName: "UpperTabStockDayCollectionViewCell", bundle: Bundle(for: UpperTabStockDayCollectionViewCell), width: { _ in return 70 }) let cellHeight: CGFloat = 50 let statusBarHeight = UIApplication.shared.statusBarFrame.height settings.style.buttonBarHeight = cellHeight + statusBarHeight settings.style.buttonBarBackgroundColor = .lightGray } override func viewDidLoad() { super.viewDidLoad() } override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { return [ StockListViewController(itemInfo: IndicatorInfo(image: UIImage(named: "main-icon"))), OneWeekStockListViewController(itemInfo: IndicatorInfo(image: UIImage(named: "monday")), dayOfTheWeek: .monday), OneWeekStockListViewController(itemInfo: IndicatorInfo(image: UIImage(named: "tuesday")), dayOfTheWeek: .tuesday), OneWeekStockListViewController(itemInfo: IndicatorInfo(image: UIImage(named: "wednesday")), dayOfTheWeek: .wednesday), OneWeekStockListViewController(itemInfo: IndicatorInfo(image: UIImage(named: "thursday")), dayOfTheWeek: .thursday), OneWeekStockListViewController(itemInfo: IndicatorInfo(image: UIImage(named: "friday")), dayOfTheWeek: .friday), OneWeekStockListViewController(itemInfo: IndicatorInfo(image: UIImage(named: "saturday")), dayOfTheWeek: .saturday), OneWeekStockListViewController(itemInfo: IndicatorInfo(image: UIImage(named: "sunday")), dayOfTheWeek: .sunday) ] } override func configure(cell: UpperTabStockDayCollectionViewCell, for indicatorInfo: IndicatorInfo) { cell.image.image = indicatorInfo.image?.withRenderingMode(.alwaysOriginal) } }
NSCoder は storyboard から生成する時に使われるものなので、コードで生成する場合は不要なはずですが、UpperTabListBaseViewController のソースはどうなってますか?
ありがとうございます!UpperTabListBaseViewControllerのコードを追加しました
ライブラリの方でストーリーボードからの生成がされる前提で作られてるんですかね?
この場合TabBarってくっつけることできないんでしょうか?
BaseButtonBarPagerTabStripViewController には init が二つあって、init(nibName:,bundle:) の方を使えば init?(coder:) は不要な気がしますが、その場合は xib ファイルからビューを生成することになるのかな…。
https://github.com/xmartlabs/XLPagerTabStrip/blob/master/Sources/BaseButtonBarPagerTabStripViewController.swift
ちなみに、init(nibName:,bundle:) や ButtonBarItemSpec.nibFile(nibName:bundle:width:) などの bundle 引数は通常 nil を指定すると Bundle.main が使われると思います。
ありがとうございます!!
解決できました
横からすみません。
XLPagerTabStrip を使う ViewController(
ButtonBarPagerTabStripViewControllerを継承したクラス) のデザインは、Storyboard を使うことを「強く」推奨されています。
もしかして、
http://harumi.sakura.ne.jp/wordpress/2020/12/23/xlpagertabstrip%E3%81%A7%E4%B8%8A%E3%82%BF%E3%83%96%E3%82%92%E5%AE%9F%E8%A3%85%E3%81%99%E3%82%8B/
を参考にされたのかもしれません。
ここには明確には書かれていませんが、おそらくこのページの実装も最終的には StoryBoard を使って実装されているように感じます。
一応、今やってみた感じだと、StoryBoard を使わなくとも実装はできそうです(強引ですが)。
その場合、init?(coder:) は使わずに、中に記載されている内容を super.viewDidLoad() の直前に持ってくる必要があるようです。
あなたの回答
tips
プレビュー