如何控制UITabBar selectedIndicatorImage的高度

时间:2019-03-04 18:42:38

标签: ios swift uitabbar

我正在尝试设置tabBar.selectedIndicatorImage的{​​{1}}的样式,但无法正确放置它的位置。它的高度应为50px,并与给定标签的 顶部 对齐。但是,它与底部对齐:

enter image description here

UITabBar

UIImage扩展名:

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
    }
}

最接近影响图像位置的方法是更改​​ 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

enter image description here

但是这似乎减少了我需要保持50px的图像高度。

我在SO上发现了很多与此相关的主题,这是我到目前为止的工作方式,但是在解决此问题方面没有发现任何有用的东西。

0 个答案:

没有答案