我正在尝试将我的应用程序转换为与 iPhone X 兼容的应用程序。我的视图控制器中有一张顶部布局为 20 像素的表格。现在下面是这两种设备的屏幕截图
现在我在 iPhone x 中得到一个带有彩色背景的白色顶栏。
如果我将表格的顶部布局设置为 0,则单元格会与我不需要的顶部栏重叠。
谁能建议是什么问题?任何建议都会很棒。
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置顶部栏(状态栏)的背景颜色。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.green
return true
}
}
or
// Set it from your view controller
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.green
}
}
这是结果:
您不应该遵循顶部布局,而是遵循新的安全区域。那么一切都应该没问题。
正如你所看到的,这个空白基本上是标题,我不知道为什么它会发生在 iPhone X 中。只是尝试覆盖方法
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
然后返回 0.01f;
尝试并完美地工作
添加 Appdelegate.m
if ([UIScreen mainScreen].bounds.size.height >= 812)
{
[application setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.clipsToBounds =YES;
self.window.frame = CGRectMake(0,40,self.window.frame.size.width,self.window.frame.size.height-20);
}
这里提供的所有答案都是很好的解决方案,但是,您也可以通过 Autolayout 来实现,方法是将 Tableview 的顶部约束设置为安全区域。这是屏幕截图。
这也适用于与底部约束相同的情况。
您也可以参考本指南了解 iPhoneX 自动布局。