在过去的十年中,我制作了数百个表视图控制器,但我不知道为什么会这样。my 的内容UITableView
卡在 my 中的 my UINavigationBar
(带有背景颜色)下UINavigationController
。我在现有项目中遇到了这个问题,但为了找出问题,我创建了一个全新的项目,其中没有任何其他内容。
- 我创建了一个项目。
- 创建了仅添加一些示例部分/行的 TestTableViewController 类。
- 从情节提要中删除了初始视图控制器。
- 添加了一个 UITableViewController 并将其类类型设置为 TestTableViewController。
- 将 UITableView 的背景颜色设置为 UIColor.systemGroupedBackgroundColor。
- 将 UITableViewCell 更改为 Basic 类型,并将其单元重用 id 设置为“Cell”。
- 告诉故事板将 TestTableViewController 嵌入 UINavigationController。
- 将该 UINavigationController 设置为初始视图控制器。
当我启动应用程序时,导航栏被隐藏(我相信是由于scrollEdgeAppearance
),当我开始从顶部边缘滚动时,导航栏开始显示。
但我需要导航栏上的背景颜色,所以:
- 在情节提要中,我将 UINavigationController 的导航栏更改为背景色。
现在 UITableView 内容卡在导航栏下。
我尝试在我的应用程序委托中配置 UINavBar 的外观:
// in AppDelegate -didFinishLaunchingWithOptions
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
[appearance configureWithOpaqueBackground];
appearance.backgroundColor = [UIColor systemRedColor];
UINavigationBar.appearance.standardAppearance = appearance;
UINavigationBar.appearance.compactAppearance = appearance;
UINavigationBar.appearance.scrollEdgeAppearance = appearance;
UINavigationBar.appearance.compactScrollEdgeAppearance = appearance;
}
但这并没有改变什么。下一个:
- 我尝试创建一个
UINavigationController
被调用的子类TestNavController
- 更新了故事板以将导航上的类设置为
TestNavController
- 向导航栏添加代码
-awakeFromNib
并显式设置样式:-viewDidLoad
// TestNavController, -awakeFromNib
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
[appearance configureWithOpaqueBackground];
appearance.backgroundColor = [UIColor systemBlueColor];
self.navigationBar.standardAppearance = appearance;
self.navigationBar.compactAppearance = appearance;
self.navigationBar.scrollEdgeAppearance = appearance;
self.navigationBar.compactScrollEdgeAppearance = appearance;
}
内容仍然停留在导航栏下。接下来,我尝试将故事板排除在等式之外,SceneDelegate.m
然后-scene:willConnectToSession:options
将故事板创建的根视图控制器替换为完全基于代码的根视图控制器:
TestTableViewController *testvc = [[TestTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
testvc.view.backgroundColor = UIColor.systemGroupedBackgroundColor;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:testvc];
self.window.rootViewController = nav;
它完全按预期工作!那么,为什么我的故事板配置的视图控制器和代码库外观设置显示错误,而基于代码的视图控制器看起来像预期的那样?我一直在不同的位置使用不同的颜色,以便我可以分辨出哪个代码具有效果。在 App Delegate 中设置红色,在 NavigationItem 上的 TestTableViewController 的 awakeFromNib 中设置蓝色。
我认为这是 scrollEdgeAppearance 的问题,但我尝试通过代码设置它,但似乎没有任何效果。
当我使用故事板时,为什么我的内容卡在导航栏下?我错过了什么?
这是下载项目的链接: https ://inadaydevelopment.com/stackoverflow/wtf.zip