How to make UITabbar selectionIndicatorImage change when changing orientation - Swift

半世苍凉 提交于 2019-12-13 05:49:09

问题


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.

How can I fixed this problem? Thank you.


回答1:


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")
    }
}


来源:https://stackoverflow.com/questions/29191574/how-to-make-uitabbar-selectionindicatorimage-change-when-changing-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!