しめ鯖日記

swift, iPhoneアプリ開発, ruby on rails等のTipsや入門記事書いてます

【Swift】SimpleTabでおしゃれなUITabBarControllerを実現する

SimpleTabというUITabBarControllerがオシャレになったようなライブラリを試してみました。

f:id:llcc:20171003173111g:plain

github.com

CocoaPodsでインストールします。

target 'MyApp' do
  use_frameworks!

  pod 'SimpleTab'
end

2017/10/3時点ではSwift4に対応していないので、Swift3で動かします。

f:id:llcc:20171003173608p:plain

まずはStoryboardだけで動かしてみます。
最初にタブバー用の適当な画像を追加します。

f:id:llcc:20171003174035p:plain

続けてStoryboard上にUITabBarControllerを設置してタブバーの画像やタイトルを設定します。
そしてUITabBarControllerのクラスをSimpleTabBarControllerにします。

f:id:llcc:20171003174056p:plain

続けて、UITabBarControllerのUITabBarのクラスをSimpleTabBarに変更します。

f:id:llcc:20171003174148p:plain

最後にUITabBarControllerに設置するUITabItemのクラスをSimpleTabBarItemに変更します。

f:id:llcc:20171003174247p:plain

以上で設定完了です。
アプリを起動すると、アニメーション付きのタブバーが表示されている事が分かります。

f:id:llcc:20171003174448g:plain

冒頭の弾むようなアニメーションは、SimpleTabBarControllerのtabBarStyleをPopTabBarStyleにする事で実現できます。

import UIKit
import SimpleTab

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        if let c = window?.rootViewController as? SimpleTabBarController {
            c.tabBarStyle = PopTabBarStyle(tabBar: c.tabBar)
        }
        
        return true
    }
}

f:id:llcc:20171003175827p:plain

タブバーのアイコンの色やテキストのフォントサイズは、tabBarStyleで変更できます。

if let c = window?.rootViewController as? SimpleTabBarController {
    c.tabBarStyle = PopTabBarStyle(tabBar: c.tabBar)
    c.tabBarStyle?.setIconColor(color: UIColor.green, forState: .selected)
}

f:id:llcc:20171003180156p:plain