求助:使用MDCTabBarView创建的Material风格TabBar无法显示问题
MDCTabBarView Not Showing Up After Following Material Docs
Hey there! Let's troubleshoot why your custom Material TabBar isn't appearing. Looking at your code, the core issue is that you've added the MDCTabBarView to your view but haven't set up auto layout constraints to define its position and size—without these, the tab bar has zero frame and stays invisible.
Here's the fixed code with proper constraints:
override func viewDidLoad() { super.viewDidLoad() let tabBarView = MDCTabBarView() tabBarView.translatesAutoresizingMaskIntoConstraints = false // Critical: Disable autoresizing mask for auto layout tabBarView.items = [ UITabBarItem(title: "Recents", image: UIImage(named: "phone"), tag: 0), UITabBarItem(title: "Favorites", image: UIImage(named: "heart"), tag: 1), // Fixed duplicate tag value ] tabBarView.preferredLayoutStyle = .scrollable // or .fixed view.addSubview(tabBarView) // Pin the tab bar to the top (adjust to bottom if needed) with standard Material height NSLayoutConstraint.activate([ tabBarView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), tabBarView.leadingAnchor.constraint(equalTo: view.leadingAnchor), tabBarView.trailingAnchor.constraint(equalTo: view.trailingAnchor), tabBarView.heightAnchor.constraint(equalToConstant: 56) ]) }
Quick additional checks to ensure full functionality:
- Verify your "phone" and "heart" images exist in your asset catalog (misnamed or missing images will result in empty tab icons).
- Confirm the Material Components library is properly installed and linked (via CocoaPods, Swift Package Manager, etc.)—a missing framework will prevent the tab bar from rendering entirely.
- I also corrected the duplicate
tagvalue in your tab bar items (tags should be unique to avoid future selection issues).
After adding these constraints, your tab bar should render as expected, matching the Material Design style you're targeting!
内容的提问来源于stack exchange,提问作者Mavis




