Detailed explanation of navigation bar in IOS 11



In addition to the addition of two new features, largetitles and searchcontroller, the navigation bar of IOS 11 may be the reason for the addition of largetitles. Its structure has changed a little compared with that of IOS 10.

Before IOS 11, the navigation bar button in the navigation bar was directly added to the navigation bar

After IOS 11, apple added a new class to manage it. You can see that titleview is directly added to the_ Uinavigationbarcontentview, uibarbuttonitem is added to the_ Uibuttonbarstackview, and_ Uibutton barstackview is added in the_ Finally, add it to the UI navigation bar, as shown in the following figure:

Due to the change of structure, in IOS 11, we customize the setting of leftbarbuttonitem, and its click area becomes very small, which makes people feel anxious, as shown in the green area below:

The specific code is as follows: the set frame is useless here, and the click area is still only the size of the original image:

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
    [btn setImage:imageWhite forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(bpBack) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor greenColor];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    leftItem.width = 60;
    self.navigationItem.leftBarButtonItem = leftItem;

In order to increase the click area, we need to increase the size of the button, and then we want to increase the size of the button by changing the content edge insets,

 btn.backgroundColor = [UIColor greenColor];
 if (@available(iOS 11.0,*)) {
     [btn setContentMode:UIViewContentModeScaleToFill];
     [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 5, 5, 20)];
 UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

In addition: if searchBar is set to titleview, the height of navigation will be abnormal (PS: push to the next interface, and there is a black area between the view of the next interface and navigation)

CGRect frame = CGRectMake(0, 0, 150, 44);
  UISearchBar *search = [[UISearchBar alloc] initWithFrame:frame];
  search.placeholder  [email protected] "search";
  search.delegate = self;
  UITextField *searchField=[search valueForKey:@"_searchField"];
  searchField.backgroundColor = [UIColor groupTableViewBackgroundColor];
//--- IOS 11 exception handling
  if(@available(iOS 11.0, *)) {
    [[search.heightAnchor constraintEqualToConstant:44] setActive:YES];
  self.navigationItem.titleView = search;

For details, please refer to:

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.