简体   繁体   English

UITabBarItem中的图像位置

[英]Image position inside UITabBarItem

I'm using code to shown a UITabBar with items, I downloaded an example that show item with system icons of "favorites" "contacts" ... 我正在使用代码显示带有项目的UITabBar,我下载了一个示例,该示例显示带有“收藏夹”,“联系人”的系统图标的项目...

UITabBarItem *favorites = [[UITabBarItem alloc] initWithTitle:nil image:a1 tag:0];
UITabBarItem *topRated = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:1];
UITabBarItem *featured = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:2];
UITabBarItem *recents = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:3];

I want to use my own images so i changed the first line (favorites), but when i setting it, the image is starting in the middle, and always stays a small place for title,even if it's empty. 我想使用自己的图像,因此我更改了第一行(收藏夹),但是当我设置它时,图像从中间开始,并且始终留在很小的标题位置,即使它是空的。

How can I controll the image possition inside those items? 如何控制这些项目中的图像位置? and disable the empty title place? 并禁用空的标题位置?

It's a fairly old question but even old questions need love :) 这是一个相当老的问题,但即使是老问题也需要爱:)

So if you're still having this problem you can use the UIViewController s tab bar item image insets: tabBarItem.imageInsets . 因此,如果仍然遇到此问题,则可以使用UIViewController的选项卡栏项目图像插入: tabBarItem.imageInsets

You set this on view controllers which you will assign to viewControllers property of the tab bar controller. 您在视图控制器上进行设置,并将其分配给选项卡栏控制器的viewControllers属性。

Here is a swift version of a subclassed tab bar controller which will always ensure all images are aligned a bit lower. 这是子类标签栏控制器的快速版本,它将始终确保所有图像对齐得更低。

override func setViewControllers(viewControllers: [AnyObject], animated: Bool) {
    super.setViewControllers(viewControllers, animated: animated)

    let array = viewControllers as! [UIViewController]
    for controller in array {
        controller.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM