iOS7 when UIsearchbar added in UINavigationBar not showing cancel button

前端 未结 7 884
误落风尘
误落风尘 2020-12-03 01:33

I add UISearchBar above UINavigationBar and set UIsearchbar showsCancelButton YES, work fine in iOS6 but in iOS7 not showing cancel button. I used below code snippet

相关标签:
7条回答
  • 2020-12-03 02:16

    For some reason iOS7 does not show the cancel button when added to a navigation bar. This also happens if you try setting it as the titleView of a navigationItem.

    You can circumvent this problem by wrapping the UISearchBar in another UIView first. Here's how I do it as a titleView:

    UISearchBar *searchBar = [UISearchBar new];
    searchBar.showsCancelButton = YES;
    [searchBar sizeToFit];
    UIView *barWrapper = [[UIView alloc]initWithFrame:searchBar.bounds];
    [barWrapper addSubview:searchBar];
    self.navigationItem.titleView = barWrapper;
    
    0 讨论(0)
提交回复
热议问题