我应该使用本机标签栏还是自定义标签栏? Swift 5 Xcode

问题描述 投票:1回答:1

我正在尝试制作类似于Instagram或TikTok的导航栏,在这里您可以点击它,也许它会给您动画(例如,在点击保持时显示比例效果。),放开后它可能会改变颜色并按比例缩小效果。我还无法弄清楚如何使用本机选项卡来获得这些结果。我的尝试是使用我创建的UIView,然后尝试将其添加到选项卡子视图中,但是当然,问题是:按钮如何像它们是选项卡按钮一样起作用?另外,当我尝试获取tabBar中每个TabBarItem的框架时,它总是给我相同的框架:Optional((0.0, 0.0, 2.0, 49.0))(我通过执行以下操作来获得框架:tab1frame = self.tabBar.items![1].value(forKey: "view") as? UIView

所以我应该怎么做?我还没有看过视频或关于如何实现这一目标的SO问题...

ios swift uitabbarcontroller uitabbar uitabbaritem
1个回答
0
投票

为了使用本机tabBar,您需要创建UITabBarController来初始化TabBar本身,然后显示视图控制器

TabBarVC示例:

override func viewDidLoad() {
        super.viewDidLoad()

        // Configure the tabBar components 
        configTabBar()
        setupMiddleButton()
    }

    // Method to configure the tabBar
    private func configTabBar() {
        self.setViewControllers(controllers, animated: true) // controllers is my array to init the VCS tapped from TabBar
        self.tabBar.barTintColor = UIColor.clear
        self.tabBar.backgroundImage = UIImage(named: TabBarStrings.backgroundImageName)?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 0)
        self.tabBar.shadowImage = UIImage(named: TabBarStrings.shadowImageName)
        self.tabBar.isTranslucent = true
        self.tabBar.tintColor = UIColor.redRestaurant
        self.tabBar.unselectedItemTintColor = UIColor.gray
    }

    // MARK: - Middle button setup

    func setupMiddleButton() {
        let tabHeight = self.tabBar.frame.size.height
        let middleBtn = UIButton(frame: CGRect(x: (self.view.bounds.width / 2)-32.5, y: -(tabHeight - 25), width: 65, height: 65)) //Set frame for an specific Button in the Tabbar 
        middleBtn.backgroundColor = UIColor.clear
        middleBtn.setBackgroundImage(UIImage(named: TabBarStrings.carImageName), for: .normal)
        middleBtn.layer.shadowOffset = CGSize(width: 0.0, height: 8.0)
        middleBtn.layer.shadowRadius = 8.0
        middleBtn.layer.shadowColor = UIColor.redRestaurant.cgColor
        middleBtn.layer.shadowOpacity = 0.68
        self.tabBar.addSubview(middleBtn)
        middleBtn.addTarget(self, action: #selector(menuButtonAction), for: .touchUpInside)
        self.view.layoutIfNeeded()
    }

这是按钮将根据您设置的内容进行操作的方式>>

func openTabBar() {
    let view = RestaurantsView()
    view.backgroundColor = UIColor.bpLightGray
    let vc1 = factory.makeRestaurantsMakeVC(view, coordinator: self)
    let item1 = UITabBarItem(title: "Sucursales", image: #imageLiteral(resourceName: "sucursal"), tag: 0)
    vc1.tabBarItem = item1

    let view2 = MainView()
    view2.backgroundColor = UIColor.bpLightGray
    let vc2 = factory.makeMainMakeVC(view2, coordinator: self)
    let item2 = UITabBarItem(title: "Menú", image: #imageLiteral(resourceName: "menu"), tag: 1)
    vc2.tabBarItem = item2

    let view3 = CarritoDeComprasView()
    view3.backgroundColor = UIColor.bpLightGray
    let vc3 = factory.makeCarritoDeComprasMakeVC(view3, coordinator: self)
    let item3 = UITabBarItem()
    vc3.tabBarItem = item3

    let view4 = HistorialView()
    view4.backgroundColor = UIColor.bpLightGray
    let vc4 = factory.makeHistorialMakeVC(view4, coordinator: self)
    let item4 = UITabBarItem(title: "Ordenes", image: #imageLiteral(resourceName: "ordenes"), tag: 3)
    vc4.tabBarItem = item4

    let view5 = ProfileView()
    view5.backgroundColor = UIColor.bpLightGray
    let vc5 = factory.makeProfileMakeVC(view5, coordinator: self)
    let item5 = UITabBarItem(title: "Perfil", image: #imageLiteral(resourceName: "Perfill"), tag: 4)
    vc5.tabBarItem = item5

    let controllers = [vc1, vc2, vc3, vc4, vc5]

    let tabBar = factory.makeTabBarVC(self, controllers: controllers)
    navigationController.setViewControllers([tabBar], animated: false)
}
    
© www.soinside.com 2019 - 2024. All rights reserved.