如何在更改方向时更改UITabbar selectionIndicatorImage - Swift

时间:2015-03-22 06:13:12

标签: ios iphone swift autolayout uitabbar

我使用图片作为tabbar selectionIndicatorImage。它在纵向方向上很有效。但是,当我更改为横向方向时,selectionIndicatorImage仍然使用Portrait的图像。因此,图像的宽度不适合横向的标签栏尺寸。

in Portrait

in Landscape

我该如何解决这个问题?谢谢。

1 个答案:

答案 0 :(得分:2)

我会根据方向更改代码中的图像。 像这样:

// the function is called everytime bounds change
override func viewDidLayoutSubviews() {
    let orientation = UIDevice.currentDevice().orientation
    if orientation == .Portrait {
        // I asssume you get images from Images.xcassets
        self.tabBarController?.tabBar.selectionIndicatorImage = UIImage(named: "image1")
    } else if orientation == .LandscapeLeft || orientation == .LandscapeRight {
        self.tabBarController?.tabBar.selectionIndicatorImage = UIImage(named: "image2")
    }
}