如何禁用水平滚动到Google Material Design for iOS上的选项卡

时间:2017-05-31 06:16:39

标签: ios swift material-design material-components

我在解决如何从Google Material Design for iOS禁用Tab栏的水平滚动时遇到问题。我打算制作一个带有3个选项卡选项的固定标签栏。

也许有人可以帮助我。提前谢谢。

示例代码:

import UIKit
import MaterialComponents
import ChameleonFramework

class ViewController: UIViewController, MDCTabBarDelegate {


  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    navigationController?.navigationBar.isTranslucent = false
    setupView()
  }

  let tabBarContainer: MDCTabBar = {

    let tabBar = MDCTabBar()

    tabBar.barTintColor = .white

    tabBar.items = [
      UITabBarItem(title: "All Activities", image: nil, tag: 0),
      UITabBarItem(title: "Bookmark", image: nil, tag: 0),
      UITabBarItem(title: "My Journal", image: nil, tag: 0)
    ]

    tabBar.itemAppearance = .titledImages
    tabBar.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
    tabBar.sizeToFit()

    tabBar.tintColor = UIColor.rgb(red: 12, green: 82, blue: 143)
    tabBar.selectedItemTintColor = UIColor.rgb(red: 12, green: 82, blue: 143)
    tabBar.unselectedItemTintColor = .lightGray

    return tabBar
  }()

  func setupView() {
    view.addSubview(tabBarContainer)
    view.addConstraintsWithFormat("H:|[v0]|", views: tabBarContainer)
    view.addConstraintsWithFormat("V:|[v0(48)]", views: tabBarContainer)
  }
}

Actual sample of the issue on simulator

3 个答案:

答案 0 :(得分:1)

可能我迟到了,但我可以通过

实现这一目标
tabBar.alignment = .justified

答案 1 :(得分:0)

如果可用,请尝试以下操作: 在tabBarContainer

tabBar.scrollView.bounces = NO;

适用于swift 3.0及以上版本

tabBar.scrollView.bounces = false

答案 2 :(得分:0)

MDCTabBar滚动会在您将其对齐方式(MDCTabBarAlignment)设置为时发生:

.leading.center.centerSelected,并且选项卡不适合给定的框架。

如果将其设置为.justified,则项目将缩小以适合框架。

myMdcTabBar.alignment = .justified