できなかった経緯
StoryBoard上でUITabBarControllerのClassをRAMAnimatedTabBarControllerに設定していたことにより、コード上でselectedIndexプロパティにアクセスできなかった
結論
新たにCustomTabBarController.swift等を作り、その中で読み込みたい外部ライブラリを継承してあげる。
やり方
1.StoryBoard上でTabBarControllerを作成する。
2.TabBarController用の.swiftファイルを作成する
3.名前は任意でつける(今回はCustomTabBarController)
4.作成したCustomTabBarControllerを下記の様に編集
ここに、今回使用したい外部ライブラリ(RAMAnimatedTabBarController)を記述する。
こうすることにより、通常通りselectedIndexプロパティにアクセスが可能。
// CustomTabBarController.swift
import RAMAnimatedTabBarController
class CustomTabBarController: RAMAnimatedTabBarController {
override func viewDidLoad() {
super.viewDidLoad()
selectedIndex = 1 // 左から2番目のタブから始める
}
}
4.TabBarControllerを選択し、Xcode右側>ShowTheIdentityInspector>CustomClassより
Classに先程作成したClassを選択(CustomTabBarController)
参考
ChatGPT