Swift - 让标签栏按钮UITabBarItem图片居中(没有文字)
对于标签栏(UITabBar),当tabBarItem不需要title只要image的时候,图片下方也是会占据一个空间的。
我们可以通过 tabBarItem.imageInsets 来设置偏移量,使得image图标居中显示。
(注意:top和bottom要设置成相反数,不然image的大小会一直改变。)
我们可以通过 tabBarItem.imageInsets 来设置偏移量,使得image图标居中显示。
(注意:top和bottom要设置成相反数,不然image的大小会一直改变。)

import UIKit
class MainTabViewController:UITabBarController
{
override func viewDidLoad()
{
super.viewDidLoad()
//一共包含了两个视图
let qqView = QQViewController()
//qqView.title = ""
let skypeView = SkypeViewController()
//skypeView.title = ""
//分别声明两个视图控制器
let qq = UINavigationController(rootViewController:qqView)
qq.tabBarItem.image = UIImage(named:"qq")
qq.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
let skype = UINavigationController(rootViewController:skypeView)
skype.tabBarItem.image = UIImage(named:"skype")
skype.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
self.viewControllers = [qq,skype]
}
}
