【发布时间】:2020-09-17 01:04:25
【问题描述】:
我想创建一个带有 6 个子视图控制器的 iOS UITabBarController,这足以使“更多”选项卡在 iPhone 11 Pro Max 上以纵向显示。作为一个实验,我想在 Swift 中以编程方式执行此操作,而不对我的 Xcode 项目的 Main.storyboard 文件进行任何修改。以下是我的整个 ViewController.swift 文件,在 macOS Catalina 10.15.4 上使用默认的“Single View App”Xcode 11.5。
import UIKit;
class ViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad();
// Do any additional setup after loading the view.
struct Band {
let badge: String;
let color: UIColor;
let systemItem: UITabBarItem.SystemItem;
}
let bands: [Band] = [
Band(badge: "0", color: .red, systemItem: .bookmarks),
Band(badge: "1", color: .orange, systemItem: .contacts),
Band(badge: "2", color: .yellow, systemItem: .downloads),
Band(badge: "3", color: .green, systemItem: .favorites),
Band(badge: "4", color: .blue, systemItem: .featured),
Band(badge: "5", color: .purple, systemItem: .history)
];
let kids: [UIViewController] = bands.map {
let viewController: UIViewController = UIViewController();
viewController.tabBarItem = UITabBarItem(tabBarSystemItem: $0.systemItem, tag: Int($0.badge)!);
viewController.tabBarItem.badgeValue = $0.badge;
viewController.view.backgroundColor = $0.color;
return viewController;
}
//Make sure all 6 kids are present and okay.
kids.forEach {print($0.tabBarItem.tag, $0.tabBarItem.badgeValue!);}
setViewControllers(kids, animated: true); //Why only getting 4 of 6 in portrait?
print("viewControllers!.count = \(viewControllers!.count)");
}
}
viewControllers!.count 应该是 6。但是当我在 iPhone 11 Pro Max 模拟器上纵向运行时,viewControllers!.count 是 4,我只看到前 4 个孩子的标签,没有“更多”标签。 (当我在已经处于横向的 iPhone 11 Pro Max 模拟器上运行该项目时,viewControllers!.count 为 6,我看到所有 6 个孩子的选项卡。)同样的事情发生在我的 iPhone 8 Plus 模拟器上。为什么我在 6 个中只有 4 个在纵向上没有“更多”,并且是否可以通过修改 ViewController.swift 文件来做我想做的事情?提前谢谢你。
【问题讨论】:
-
您的代码似乎没问题,我得到了 4 + 1 个菜单,另外还有 2 个菜单。
-
谢谢你看这个,弗兰肯斯坦。我添加了我的 iPhone 11 Pro Max 模拟器的纵向屏幕截图,以向您展示我所看到的。
-
您可以尝试清除模拟器的数据并再次运行吗?另外,请在再次运行之前尝试清除派生数据并清理项目。
-
谢谢,我删除了 DerivedData 文件夹,在 Xcode 中执行“Product -> Clean Build Folder”,从模拟器中删除应用程序,然后重试。我仍然只有 4 个标签。同样的事情也发生在其他模拟器中(例如 iPhone 8 Plus 纵向)。
标签: ios swift xcode uitabbarcontroller programmatically