Search code examples
iosios11iphone-x

Wrong views position on iPhone X


My app looked well on all devices, until I tested it on new iPhone X (See the attached screenshot). Search bar is under navigation bar, and it's awful. I tried playing with new safe area insets, edgesForExtendedLayout, but with no success. Maybe someone can help with this issue.

enter image description here


Solution

  • After some investigations I figured out how to fix my issue. I had to align the search view according to the safe area. See the solution below:

     if (@available(iOS 11.0, *))
     {
         [self.searchView setTranslatesAutoresizingMaskIntoConstraints:NO];
         UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
    
         [NSLayoutConstraint activateConstraints:@[
                                                   [self.searchView.topAnchor constraintEqualToAnchor:guide.topAnchor],
                                                   [self.searchView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor],
                                                   [self.searchView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor],
                                                   [NSLayoutConstraint constraintWithItem:self.searchView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44]
                                                   ]];
      }
    

    Now everything looks great on all devices. Hope this helps someone else with the similar issue. Cheers.