How to make UITabbar selectionIndicatorImage chang

2019-08-28 07:49发布

I uses an image for my tabbar selectionIndicatorImage. It works great in Portrait orientation. However, when I change to Landscape orientation, the selectionIndicatorImage is still using the image for Portrait. Therefore, the image's width doesn't fit my Tab bar size in Landscape.

in Portrait

in Landscape

How can I fixed this problem? Thank you.

1条回答
不美不萌又怎样
2楼-- · 2019-08-28 08:13

I would change the image from code depending on the orientation. Like this:

// 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")
    }
}
查看更多
登录 后发表回答