【问题标题】:How to create floating Tab Bar Swift如何创建浮动标签栏 Swift
【发布时间】:2021-04-22 13:39:42
【问题描述】:

我正在尝试创建一个 Pinterest 风格的标签栏。是否可以以这种方式自定义 UITabBar 或者您是否必须编写一个单独的视图来坐在所有内容之上,如果是这样,您将如何做到这一点?它基本上只是用圆角和阴影向上移动。您将如何调整标签栏的宽度并将其向上移动?

Image of Tab Bar

【问题讨论】:

  • 看看这个朋友,也许这会起作用 -> medium.com/@gemix95/…
  • 它真的是一个 TabBar,还是一个添加在顶部的 UIWindows,它可能会委托给一个主视图控制器(可能是一个 UITabBarController)。
  • @emrcftci 哇,这比我预期的要容易得多。谢谢!
  • @emrcftci 你能把它作为答案发布,以便我将其标记为正确
  • 实际上这不是一个答案,但我会修改它并作为答案发布,我很高兴能够提供帮助。

标签: swift uitabbar tabbar pinterest


【解决方案1】:

我创建了一个浮动标签栏库。它看起来不像 Pinterest 的,但您可以修改绘图代码以改变它的外观。

Here's the repo.

如果你想从头实现它,你必须创建自己的视图控制器子类,以及标签栏的 UIView 子类,并自己管理“页面”切换。 UITabBarController 没有为此提供足够的自定义。

【讨论】:

    【解决方案2】:

    如果你想制作“RoundedTabbar”,你可以像这样创建一个 ->

    import UIKit
    
    class RoundedTabBarController: UITabBarController {
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        let layer = CAShapeLayer()
        layer.path = UIBezierPath(roundedRect: CGRect(x: 30, y: tabBar.bounds.minY + 5, width: tabBar.bounds.width - 60, height: tabBar.bounds.height + 10), cornerRadius: (tabBar.frame.width/2)).cgPath
        layer.shadowColor = UIColor.lightGray.cgColor
        layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
        layer.shadowRadius = 25.0
        layer.shadowOpacity = 0.3
        layer.borderWidth = 1.0
        layer.opacity = 1.0
        layer.isHidden = false
        layer.masksToBounds = false
        layer.fillColor = UIColor.white.cgColor
      
        tabBar.layer.insertSublayer(layer, at: 0)
    
        if let items = tabBar.items { 
            items.forEach { item in 
                item.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -15, right: 0) 
            }
        }
    
        tabBar.itemWidth = 30.0
        tabBar.itemPositioning = .centered
      }
    }
    

    如果您使用故事板,请不要忘记输入身份检查器中的类名。

    PS:我正在分享 Emmanuele Corporente's Medium article 内容作为答案,请查看原始文章并鼓掌!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      相关资源
      最近更新 更多