ios 为什么UINavigationItem显示菜单按钮?

yhqotfr8  于 2023-05-30  发布在  iOS
关注(0)|答案(2)|浏览(214)

我有UINavigationItem和UISearachController;

#warning - addded UISearchController
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.navigationItem.searchController = self.searchController;
self.navigationItem.hidesSearchBarWhenScrolling = NO;
if (@available(iOS 16.0, *)) {
    self.navigationItem.preferredSearchBarPlacement = UINavigationItemSearchBarPlacementInline;
    
}
self.searchController.searchBar.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.definesPresentationContext = YES;
self.searchController.obscuresBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.placeholder = @"Search";
self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;
[self.searchController.searchBar sizeToFit];

当我点击导航栏中的搜索按钮时,我可以看到一个搜索字段和一些带有三个点的按钮(看起来像UIContextMenu,点击日志中的此按钮后,我看到“[UILog] Called -[UIContextMenuInteraction updateVisibleMenuWithBlock:],而没有上下文菜单可见。这不会做任何事情”)。

问题:
1.为什么我可以在导航中看到这个?(我不加这个)
1.如何从导航视图中删除此按钮

ffscu2ro

ffscu2ro1#

这可能是因为导航栏在上下文菜单中 Package 了所有额外的按钮。这是WWDC 2022中引入的新功能。您可以查看此视频以了解更多细节:https://developer.apple.com/videos/play/wwdc2022/10069/

1szpjjfi

1szpjjfi2#

所以这个元素被命名为“溢出支持按钮”。(感谢@Mozz)
如果您向navigationItem.rightBarButtonItems中添加任何项目,那么在打开搜索栏(self.navigationItem.preferredSearchBarPlacement = UINavigationItemSearchBarPlacementInline;)
在我的例子中,我重新实现了rightBarButtonItem,这允许我省略“溢出按钮”的表示

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:self.modeButton];
if (@available(iOS 16.0, *)) {
    UIBarButtonItemGroup *group = [[UIBarButtonItemGroup alloc] init];
    group.alwaysAvailable = YES;
    group.representativeItem = barButton;
    self.navigationItem.pinnedTrailingGroup = group;
} else {
    self.navigationItem.rightBarButtonItem = barButton;
}

相关问题