菜鸟教程小白 发表于 2022-12-11 19:12:46

iOS 11 在 leftBarButtonItem 旁边的导航栏中显示 searchBar


                                            <p><p>我正在尝试使新的 iOS-11 searchBar 方法正常工作,但没有成功。 searchBar 出现了,但显示在左栏按钮下方,而不是旁边:</p>

<p> <a href="/image/8RN7J.png" rel="noreferrer noopener nofollow"><img src="/image/8RN7J.png" alt="enter image description here"/></a> </p>

<p>在横向模式下也是如此。
我使用的代码是:</p>

<pre><code>self.searchResults = [ init];
self.searchResults.delegate = self;
searchController = [ initWithSearchResultsController:self.searchResults];
searchController.searchResultsUpdater = self.searchResults;
searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;

self.navigationItem.leftBarButtonItem = [
    initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                         target:self
                         action:@selector(swapToListView:)];
if () {
    // only available from IOS-11 onwards
    self.navigationItem.title = nil;
    self.navigationItem.titleView = nil;
    ;

} else {
    // Fallback on earlier versions
    self.navigationItem.titleView = searchController.searchBar;
}
</code></pre>

<p>旧代码在 iOS-11 之前的系统上运行良好,但在 iOS-11 上存在其他问题,所以我更喜欢使用新方法。
有人知道我在这里做错了什么吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果你想把搜索栏放在左侧导航项旁边,那么不管iOS版本如何,让你的搜索栏<code>navigationItem</code>的<code>titleView</code>,不要使用<code>UINavigationItem</code> 的 <code>searchController</code> 属性。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 11 在 leftBarButtonItem 旁边的导航栏中显示 searchBar,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47138413/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47138413/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 11 在 leftBarButtonItem 旁边的导航栏中显示 searchBar