ios - Material 组件选项卡 Swift 中的项目大小

标签 ios swift tabs

enter image description here

我想自动调整项目的大小以适应 UITabBarItem 的数量。但是,项目的大小不会自动调整。
我想要两个充满标签栏的项目,其中三个充满标签栏。

目前,标签栏有一个项目,但标签栏未满,因为大小是恒定的。

enter image description here

let tabBar = MDCTabBar(frame: CGRect(x: 0, y: 88, width: 414, height: 40))

override func viewDidLoad() {
    super.viewDidLoad()

    configureTabBar()
}

func configureTabBar() {
    tabBar.items = [
        UITabBarItem(title: "one", image: .none, tag: 0),
        UITabBarItem(title: "two", image: .none, tag: 0),
    ]

    tabBar.itemAppearance = .titledImages
    tabBar.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
    tabBar.sizeToFit()
    tabBar.tintColor = UIColor.red
    tabBar.barTintColor = UIColor.white
    tabBar.rippleColor = UIColor.red
    tabBar.bottomDividerColor = UIColor.lightGray
    tabBar.unselectedItemTintColor = UIColor.gray
    tabBar.selectedItemTintColor = UIColor.red

    view.addSubview(tabBar)
}

我必须添加哪些选项才能使项目像“ Material 组件”选项卡中的示例一样填充选项卡栏?

最佳答案

如何在 configureTabBar() 中添加以下代码

tabBar.alignment = .justified

编辑:
MDCTabBar 具有称为“对齐”的属性,因此根据 MaterialComponents MDCTabBar.h 和 MDCTabBar.m 中的文档:

Property 'alignment' is Horizontal alignment of tabs within the tab bar. Changes are not animated. Default alignment is MDCTabBarAlignmentLeading. The default value is based on the position and is recommended for most applications



对齐是 MDCTabBarAlignment 枚举类型,有四种枚举:

  1. MDCTabBarAlignmentLeading -> Items are aligned on the leading edge and sized to fit their content

  2. MDCTabBarAlignmentJustified -> Items are justified to equal size across the width of the screen. Overscrolling is disabled for this alignment.

  3. MDCTabBarAlignmentCenter -> Items are sized to fit their content and center-aligned as a group. If they do not fit in view, they will be leading-aligned instead.

  4. MDCTabBarAlignmentCenterSelected -> Tabs are center-aligned on the selected item.

关于ios - Material 组件选项卡 Swift 中的项目大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56964794/

相关文章:

带有可选默认参数的 SwiftUI View

iphone - 使用 NSPredicate 获取核心数据对象数组?

ios - 类型 'ViewController' 不符合协议(protocol) 'UITableViewDataSource'

ios - 使用滑动手势更改 View

python - 带有表格的 CSS 中的选项卡 View

德尔福编辑器。用制表符代替空格

ios - Swift 中的 XLForm 验证

ios - 无法将存档导出到 ipa(未找到适用的设备)

ios - 更改 UITableViewCell 中元素的动态高度约束

css - 如何更改我的选项卡使下面的内容缩进?