问题
I would like the tab bar items in my app to evenly take up all of the tab bar whether in portrait or landscape. I have five tabs on my tab bar. The code below does not accomplish this. What am I doing wrong? I noticed that the screenSize.size.height actually references the width and I am not sure why that is.
Any help is much appreciated.
Thank you
UITabBar *tabBar = self.tabBarController.tabBar;
CGRect screenSize = [[UIScreen mainScreen] bounds];
float width;
if (self.isShowingLandscapeView){
width = screenSize.size.height/5;
}
else {
width = (screenSize.size.width/5);
}
[tabBar setItemWidth:width];
回答1:
In your viewDidLoad()
, set
tabBar.itemPositioning = .fill
Hope this helps someone out.
回答2:
In Swift 4 you can also enter the following in viewDidLoad
method
tabBar.alignment = .justified
回答3:
In the tab bar attribute inspector in your story board file. Change the item positioning property to centered. Two fields additional fields item width and item spacing will appear. next to item spacing select custom and enter a value that satisfies your spacing requirement. Hope that helps.
来源:https://stackoverflow.com/questions/27711237/how-to-set-uitabbaritem-widths-evenly-to-take-up-all-of-tab-bar