ios - 如何控制 UITabBar selectedIndicatorImage 的高度
问题描述
我正在尝试设置我的样式,tabBar.selectedIndicatorImage
但UITabBar
无法正确定位。它应该是 50 像素高并与给定选项卡的顶部对齐。但是,它与底部对齐:
class MenuTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setTabs()
styleTabBar()
}
func setTabs() {
let tab1 = GradientNavigationController(rootViewController: FeedViewController())
let tab2 = GradientNavigationController(rootViewController: NotificationsTableViewController())
let tab3 = GradientNavigationController(rootViewController: SearchViewController())
let tab4 = GradientNavigationController(rootViewController: CallsTableViewController())
UITabBarController().setViewControllers([tab1, tab2, tab3, tab4], animated: false)
}
// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 0 {
if let feedNav = children[0] as? UINavigationController, let feedVC = feedNav.viewControllers[0] as? FeedViewController {
feedVC.tableView.reloadData()
}
}
}
func styleTabBar() {
// Set the tab bar separator
UITabBar.appearance().shadowImage = UIImage.colorForNavBar(color:.white) //This sets the TabBar top separator color
}
override func viewDidAppear(_ animated: Bool) {
let numberOfItems = CGFloat(tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: 50)
let selectionBackgroundImage = UIImage.imageWithColor(color: .lightGrey3, size: tabBarItemSize)
selectionBackgroundImage.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0), resizingMode: .tile)
tabBar.selectionIndicatorImage = selectionBackgroundImage
}
}
UIImage 扩展:
class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
我最接近甚至影响图像位置的方法是通过更改扩展CGRect
中的原点:UIImage
let rect: CGRect = CGRect(x: 0, y: -25, width: size.width, height: size.height)
但这似乎只是降低了我需要保持在 50px 的图像的高度。
我在 SO 上发现了很多关于此的主题,这就是我走到这一步的方式,但我没有发现任何东西可以解决这个问题。
解决方案
推荐阅读
- python - 在标签 tkinter 中显示日历中的日期
- amazon-web-services - AWS Amplify AppSync GraphQL @Auth 指令:如何禁止为公共用户列出元素
- libc - 在 efi shell 应用程序中包含 edk2-libc
- java - COS上使用Aspera Java SDK访问文件的问题
- angular - Angular 接口中的可选 id 提供类型编号的参数 undefined 不能分配给类型编号的参数
- reactjs - NextJS - Google 字体未加载或显示在网站上
- c++ - 当行长于 ColumnLimit 时,Clang-format align AlignConsecutiveAssignments 会中断
- python - 如何在python中记录转换?
- shell - 移动所有 23 小时生成的文件。在它们首次创建之后
- bash - 如何从命令输出(bash)中复制一些参数?