0

我正在使用它来创建导航栏,但在 iPhoneX 中,由于安全区域视图布局,它会中断

 navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIApplication sharedApplication].keyWindow.rootViewController.view.frame.size.width, 50)];
    navItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
    cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
    navItem.leftBarButtonItem = cancelBtn;
    [navbar setItems:@[navItem]];

在此处输入图像描述

我们如何在 Objective-C 中做到这一点来支持 iPhoneX?

4

1 回答 1

0

这是修复导航栏的解决方法

- (void)viewDidLayoutSubviews{
    int start = self.view.safeAreaInsets.top;
       UINavigationBar * navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, start, [UIScreen mainScreen].bounds.size.width, 50)];
         navbar.prefersLargeTitles = YES;

       UINavigationItem*  navItem = [[UINavigationItem alloc] initWithTitle:@"Title"];

         [navbar setItems:@[navItem]];

         [self.view addSubview:navbar];

}
于 2020-02-26T16:15:45.983 回答