菜鸟教程小白 发表于 2022-12-13 11:07:50

ios - 搜索栏打开/关闭时隐藏/显示导航栏上的后退按钮


                                            <p><p>所以我有一个导航栏,上面有一个“返回”按钮,右边有一个 UISearchBar:</p>

<p>![在此处输入图片描述]</p>

<p>当 UISearchBar 打开/显示时,取消按钮隐藏/显示:</p>

<p>![在此处输入图片描述]</p>

<p>我想要的是当 UISearchBar 打开时,我希望它基本上“覆盖”后退按钮。当它关闭时,我希望它“揭开”后退按钮。到目前为止,这是我的代码:</p>

<pre><code>#import &#34;SearchViewController.h&#34;

@interface SearchViewController ()

@end

- (void)viewDidLoad {
    ;
    if ()
      self.edgesForExtendedLayout = UIRectEdgeNone;

    UISearchBar *searchBar = ;
    searchBar.showsCancelButton = NO;
    ;
    searchBar.delegate = self;

    self.navigationItem.titleView = searchBar;
    self.navigationItem.rightBarButtonItem = [ initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];

}

- (void)didReceiveMemoryWarning {
    ;
}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    ;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    ;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    ;
    ;
}

@end
</code></pre>

<p>我尝试做的是: <code>self.navigationItem.hidesBackButton = NO/YES;</code> 在 searchBarTextDidBeginEditing/searchBarTextDidEndEditing 中,但这会留下后退按钮为空的地方:</p>

<p>![在此处输入图片描述]</p>

<p>有没有办法让搜索栏延伸到后退按钮上?谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>尝试这样做</p>

<pre><code>self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
</code></pre>

<p>或</p>

<pre><code>self.navigationItem.backBarButtonItem=nil;
</code></pre>

<p>更新代码:</p>

<pre><code>@interface SearchViewController ()
{
    UIBarButtonItem *backButtonItem;
    UIBarButtonItem *negativeSpacer;
}
@end

@implementation SearchViewController

- (void)viewDidLoad {
    ;
    // Do any additional setup after loading the view.
    UISearchBar *searchBar = ;
    searchBar.showsCancelButton = NO;
    ;
    searchBar.delegate = self;

    self.navigationItem.titleView = searchBar;
    self.navigationItem.rightBarButtonItem = [ initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];

    self.navigationItem.backBarButtonItem=nil;
    self.navigationItem.hidesBackButton=YES;

    UIButton *backButton = [ initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
    UIImage *backImage = ;
    ;
    ;
    ;
    ;
    backButtonItem = [ initWithCustomView:backButton];

    negativeSpacer = [ initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    ;

    self.navigationItem.leftBarButtonItems = ;
}

- (void)didReceiveMemoryWarning {
    ;
    // Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    ;
    self.navigationItem.leftBarButtonItems = nil;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    ;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    ;
    ;
    self.navigationItem.leftBarButtonItems = ;
}
- (IBAction)backButtonPressed:(id)sender{
    ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 搜索栏打开/关闭时隐藏/显示导航栏上的后退按钮,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31216483/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31216483/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 搜索栏打开/关闭时隐藏/显示导航栏上的后退按钮