0

I'm using this for creating navbar but in iPhoneX it getting break due to safe area view layout

 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]];

enter image description here

How can we do this in Objective-C to support iPhoneX?

Karthik Mandava
  • 507
  • 1
  • 14
  • 27

1 Answers1

0

here is the workaround to fix navigation bar

- (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];

}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49