创建自定义UITabBar控制器并使用分隔符突出显示所选选项卡

问题描述 投票:0回答:2

请查看以下屏幕截图及其对tabbar的规范。

  1. 将拐角半径仅显示在左上角并在其上应用阴影。
  2. 将曲线应用于右上角。
  3. 显示带有红色字体颜色的选定选项卡,并在下方显示分隔符。

因为我在过去的三天里被困住了。

任何帮助,将不胜感激。

UITabbar cotroller

ios swift uitabbarcontroller custom-controls uitabbar
2个回答
0
投票

把代码放在AppDelegate didFinishLaunching方法中: -

let tabBarController = self.window!.rootViewController as! UITabBarController
        let tabBar = tabBarController.tabBar

        DispatchQueue.main.async {
            tabBar.selectionIndicatorImage = UIImage().createSelectionIndicatorFill(fillColor:.red, lineColor:.blue,size: CGSize(width:tabBar.frame.width/CGFloat(tabBar.items!.count), height:tabBar.frame.height), lineWidth: 1.0)
            tabBar.unselectedItemTintColor = customColor
        }

并扩大UIImage

extension UIImage {
    func createSelectionIndicatorFill(fillColor: UIColor,lineColor:UIColor,size: CGSize, lineWidth: CGFloat) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        fillColor.setFill()
        UIRectFill(CGRect(x:0, y:0, width:size.width, height:size.height - lineWidth))

        lineColor.setFill()

        UIRectFill(CGRect(x:0, y:size.height - lineWidth, width:size.width, height:lineWidth))

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }

0
投票

您可以通过创建自定义选项卡栏并添加选项卡的每个方面视图控制器来实现它,以使用这些步骤将其添加为子vc。

  1. 使用视图创建自定义视图(将按钮和分隔符添加为子视图)。您可以使用UIBezierPath或使用二进制图像来实现顶角的曲线,并管理按钮和分隔符行为,如颜色。
  2. 在视图上添加第一个选项卡的VC加载 self.add(asChildViewController: firstViewController)
  3. 并且在每个按钮上添加/删除(显示/隐藏)子视图控制器,就像这样 //MARK: - Add Child View Controller private func add(asChildViewController viewController: UIViewController) { // Add Child View Controller addChildViewController(viewController) // Add Child View as Subview view.addSubview(viewController.view) // Configure Child View viewController.view.frame = view.bounds viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] // Notify Child View Controller viewController.didMove(toParentViewController: self) } //MARK: - Remove Child View Controller private func remove(asChildViewController viewController: UIViewController) { // Notify Child View Controller viewController.willMove(toParentViewController: nil) // Remove Child View From Superview viewController.view.removeFromSuperview() // Notify Child View Controller viewController.removeFromParentViewController() }
© www.soinside.com 2019 - 2024. All rights reserved.