简体   繁体   English

自定义标签栏 iOS

[英]Custom Tab Bar iOS

在此处输入图片说明

I want to create a custom tab bar like the image shows.我想创建一个如图所示的自定义标签栏。 I don't know how to begin subclassing UITabBar to get this result.我不知道如何开始UITabBar以获得这个结果。 What If I simply have a Root UIViewController , and create the perception of tab bar through interface builder (a UIView, images, circle button, etc) and add ContainerView 's for map and list UIViewController 's?如果我只有一个 Root UIViewController ,并通过interface builder (UIView、图像、圆形按钮等)创建标签栏的感知并为地图添加ContainerView并列出UIViewController会怎样? What are the downfalls to this approach, if any?这种方法的缺点是什么(如果有的话)? Resizing issue (looks good on iPhone 6 but not iPhone X) since there is no tab bar to dynamically resize maybe?调整大小问题(在 iPhone 6 上看起来不错,但在 iPhone X 上看起来不错),因为没有标签栏可以动态调整大小?

When you break any UIKit component down, like UITabBar , all you will ever find are other UIKit components, that are all available to us.当您分解任何UIKit组件时,例如UITabBar ,您将发现的只是其他UIKit组件,它们都可供我们使用。 And while it's nice to use Apple's prebuilt stuff sometimes, they don't always like us meddling with them and they often hide the very behaviors that we want to modify.虽然有时使用 Apple 的预构建东西很好,但他们并不总是喜欢我们干涉他们,他们经常隐藏我们想要修改的行为。

You should have absolutely zero fear of creating your own UI components, like a custom tab bar, which for our purposes is nothing more than a UIView container with either UIButton or UIImageView subviews.您应该完全零担心创建自己的 UI 组件,例如自定义标签栏,就我们而言,它只不过是带有UIButtonUIImageView子视图的UIView容器。 If you have a container view controller (that contains the child view controllers), put the tab bar in there and you will have a legitimate tab bar that will always be superimposed over content view controllers, except for presented view controllers (which is exactly the behavior we want).如果你有一个容器视图控制器(包含子视图控制器),把标签栏放在那里,你将有一个合法的标签栏,它总是叠加在内容视图控制器上,除了呈现的视图控制器(这正是我们想要的行为)。

And as far as iPhone X is concerned and safe areas, just apply them how you normally would any view because that's all your custom tab bar is:就 iPhone X 和安全区域而言,只需按照您通常的任何视图应用它们,因为这就是您的所有自定义标签栏:

if #available(iOS 11.0, *) {

    tabBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    tabBar.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor).isActive = true
    tabBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

} else {

    tabBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    tabBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    tabBar.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor).isActive = true

}

tabBar.heightAnchor.constraint(equalToConstant: 44).isActive = true

And to your concern about resizing, if you give your tab bar constraints (like you should be doing anyway) in the container view controller, it will stretch and rotate in every possible way without fail.对于您对调整大小的担忧,如果您在容器视图控制器中设置标签栏约束(就像您无论如何都应该做的那样),它将以各种可能的方式拉伸和旋转,而不会失败。

you can Use ESTabBarController it's easy to use and will solve your problems ..:)您可以使用ESTabBarController它易于使用并且可以解决您的问题..:)

https://github.com/eggswift/ESTabBarController https://github.com/eggswift/ESTabBarController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM