简单问题,如何去除选项卡条目的文本并仅显示图像?
我希望选项卡项与Instagram应用程序中的类似:
在Xcode 6的检查器中,我删除了标题并选择了@2x(50px)和@3x(75px)的图像。但是,该图像没有使用已删除文本的自由空间。有什么想法可以实现与Instagram应用程序中相同的选项卡条目图像?
简单问题,如何去除选项卡条目的文本并仅显示图像?
我希望选项卡项与Instagram应用程序中的类似:
在Xcode 6的检查器中,我删除了标题并选择了@2x(50px)和@3x(75px)的图像。但是,该图像没有使用已删除文本的自由空间。有什么想法可以实现与Instagram应用程序中相同的选项卡条目图像?
你应该使用 UITabBarItem
的 imageInsets
属性进行操作。以下是样例代码:
let tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "more")
tabBarItem.imageInsets = UIEdgeInsets(top: 9, left: 0, bottom: -9, right: 0)
UIEdgeInsets
中的数值取决于您的图像大小。这是我的应用程序中该代码的结果:
// Remove the titles and adjust the inset to account for missing title
for(UITabBarItem * tabBarItem in self.tabBar.items){
tabBarItem.title = @"";
tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
}
以下是在故事板中的操作步骤。
清除标题文本,并设置图像插图,如下方屏幕截图所示:
请记住,图标大小应遵循Apple设计准则。
这意味着您应该拥有@1x尺寸为25px x 25px,@2x尺寸为50px x 50px,@3x尺寸为75px x 75px的图标。
使用将每个UITabBarItem
的title
属性设置为""
并更新imageInsets
的方法,在视图控制器self.title
被设置时将无法正常工作。例如,如果UITabBarController
的self.viewControllers
嵌入了UINavigationController
中,并且您需要在导航栏上显示标题。在这种情况下,请直接使用self.navigationItem.title
而不是self.title
来设置UINavigationItem
的标题。
for tabBarItem in tabBar.items!
{
tabBarItem.title = ""
tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
}
ddiego的Swift版本答案
iOS 11兼容
在设置viewController标题后,在每个视图控制器的第一个子项的viewDidLoad中调用此函数。
最佳实践:
如@daspianist在评论中建议的那样,创建一个类似于BaseTabBarController的子类:UITabBarController,UITabBarControllerDelegate,并将此函数放入子类的viewDidLoad中。
func removeTabbarItemsText() {
var offset: CGFloat = 6.0
if #available(iOS 11.0, *), traitCollection.horizontalSizeClass == .regular {
offset = 0.0
}
if let items = tabBar.items {
for item in items {
item.title = ""
item.imageInsets = UIEdgeInsets(top: offset, left: 0, bottom: -offset, right: 0)
}
}
}
class BaseTabBarController: UITabBarController, UITabBarControllerDelegate
,并将此函数放在子类的 viewDidLoad
中。 - daspianistiOS 11对许多解决方案产生了影响,因此我通过子类化UITabBar并覆盖layoutSubviews来解决了iOS 11上的问题。
class MainTabBar: UITabBar {
override func layoutSubviews() {
super.layoutSubviews()
// iOS 11: puts the titles to the right of image for horizontal size class regular. Only want offset when compact.
// iOS 9 & 10: always puts titles under the image. Always want offset.
var verticalOffset: CGFloat = 6.0
if #available(iOS 11.0, *), traitCollection.horizontalSizeClass == .regular {
verticalOffset = 0.0
}
let imageInset = UIEdgeInsets(
top: verticalOffset,
left: 0.0,
bottom: -verticalOffset,
right: 0.0
)
for tabBarItem in items ?? [] {
tabBarItem.title = ""
tabBarItem.imageInsets = imageInset
}
}
}
我在我的BaseTabBarController的viewDidLoad中使用了以下代码。请注意,在我的示例中,我有5个选项卡,并且所选图像将始终为base_image +“_selected”。
// Get tab bar and set base styles
let tabBar = self.tabBar;
tabBar.backgroundColor = UIColor.whiteColor()
// Without this, images can extend off top of tab bar
tabBar.clipsToBounds = true
// For each tab item..
let tabBarItems = tabBar.items?.count ?? 0
for i in 0 ..< tabBarItems {
let tabBarItem = tabBar.items?[i] as UITabBarItem
// Adjust tab images (Like mstysf says, these values will vary)
tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -6, 0);
// Let's find and set the icon's default and selected states
// (use your own image names here)
var imageName = ""
switch (i) {
case 0: imageName = "tab_item_feature_1"
case 1: imageName = "tab_item_feature_2"
case 2: imageName = "tab_item_feature_3"
case 3: imageName = "tab_item_feature_4"
case 4: imageName = "tab_item_feature_5"
default: break
}
tabBarItem.image = UIImage(named:imageName)!.imageWithRenderingMode(.AlwaysOriginal)
tabBarItem.selectedImage = UIImage(named:imageName + "_selected")!.imageWithRenderingMode(.AlwaysOriginal)
}
for tabBarItems in tabBar.items!
代替 for var i = 0; i < tabBar...
。 - Jesse OnolemenSwift 4做法
我通过实现一个函数来完成技巧,该函数接收TabBarItem并对其进行一些格式化。
将图像下移一点以使其更加居中,并隐藏Tab Bar的文本。这比仅将其标题设置为空字符串要好,因为当您还有NavigationBar时,TabBar在选择时会恢复viewController的标题。
func formatTabBarItem(tabBarItem: UITabBarItem){
tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.clear], for: .selected)
tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.clear], for: .normal)
}
最新的语法
extension UITabBarItem {
func setImageOnly(){
imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.clear], for: .selected)
setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.clear], for: .normal)
}
}
只需在您的 tabBar 中使用它:
tabBarItem.setImageOnly()
一份基于@korgx9答案的Swift编写的最小安全UITabBarController扩展:
extension UITabBarController {
func removeTabbarItemsText() {
tabBar.items?.forEach {
$0.title = ""
$0.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
}
}
}
""
作为标题,也许? - holex