IOS7:uisearchdisplaycontroller始终显示范围栏 [英] IOS7 : uisearchdisplaycontroller always show scope bar

查看:118
本文介绍了IOS7:uisearchdisplaycontroller始终显示范围栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要实现的是让我的范围栏永远不会消失。

Basically what I'm trying to achieve is to have my scope bar to never disappear.

环境:IOS 7,故事板,在视图控制器中我有一个搜索栏和搜索显示控制器和单独的表格视图(搜索栏不在表格内)

Environment : IOS 7, storyboard, inside a view controller I have a "search bar and search display controller" and a separate tableview (the searchbar is not inside the table)

在视图controller.h内部

Inside the view controller.h

@property (nonatomic, strong) IBOutlet UISearchBar *candySearchBar;

在视图内部控制器.m

Inside the view controller.m

@synthesize candySearchBar;

我尝试的内容:在自定义搜索栏类中

What I tried : inside a custom search bar class

- (void) setShowsScopeBar:(BOOL) showsScopeBar
{
    if ([self showsScopeBar] != showsScopeBar) {
        [super invalidateIntrinsicContentSize];
    }
    [super setShowsScopeBar:showsScopeBar];

    [super setShowsScopeBar: YES]; // always show!

    NSLog(@"setShowsScopeBar searchbar");
    NSLog(@"%hhd", showsScopeBar);
}

searchBarDidEndEditing

视图控制器中的相同内容,但随后

Same thing in the view controller, but then

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [candySearchBar setShowsScopeBar:YES];
    [candySearchBar sizeToFit];
}

我希望我的问题很明确,我试过很多解决方案在互联网上发布,他们中的大多数都谈论了setshowsscopebar,但它似乎没有用。 setshowscopebar中的日志输出为1,但是示波器栏仍然没有显示。

I hope my question is clear, I tried many solutions posted all over the internet, most of them talk about the setshowsscopebar, but it doesn't seem to work. The output of the log in setshowscopebar is 1, but the scopebar is still not shown.

我仍然认为自己不熟悉代码,故障仍然是新手错误。

I still consider myself to be new to the code, the fault can still be a newbie mistake.

编辑:视图控制器中的另一段代码,因为你可以看到我在盲目搜索:

edit : another piece of code in the view controller, as you can see i'm searching blind:

-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
    self.searchDisplayController.searchBar.showsCancelButton = YES;
    self.searchDisplayController.searchBar.showsScopeBar = YES;
    controller.searchBar.showsScopeBar = TRUE;
    controller.searchBar.frame = CGRectMake(0, 149, 768, 88);
    UIButton *cancelButton;
    UIView *topView = self.searchDisplayController.searchBar.subviews[0];
    for (UIView *subView in topView.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            cancelButton = (UIButton*)subView;
        }
    }
    if (cancelButton) {
        //Set the new title of the cancel button
        [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
        [cancelButton setEnabled:YES];
        controller.searchBar.showsScopeBar = YES;
        //candySearchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Flags", @"Listeners", @"Stations", nil];
    }
    NSLog(@"%@",NSStringFromCGRect(controller.searchBar.frame));
    NSLog(@"%@",NSStringFromCGRect(controller.searchBar.bounds));
    NSLog(@"%hhd@",controller.searchBar.hidden);
}


推荐答案

你试过的代码不会因为苹果已经改变了 UISearchBar 的行为,以便在返回普通视图时隐藏范围。将此方法添加到自定义 searchBar 类。

The code you tried will not work in iOS7 onward because apple has changed it behavior of UISearchBar to hide the scope when return to normal view. Add this method to your custom searchBar class.

-(void)layoutSubviews
{
    [super layoutSubviews];
    if([[UIDevice currentDevice].systemVersion floatValue]>=7.0) {
         //Get search bar with scope bar to reappear after search keyboard is dismissed
         [[[[self.subviews objectAtIndex:0] subviews] objectAtIndex:0] setHidden:NO];
         [self setShowsScopeBar:YES];
     }
}

直接访问索引处的对象可能会导致iOS6中的应用程序崩溃因为iOS6和iOS7之间的视图层次结构不同,为了避免这种情况,只在iOS7时才添加此内容。

Directly accessing object at index may crash the app in iOS6 because of difference in view hierarchy between iOS6 and iOS7, to avoid this, add this inside if condition only when its iOS7.

此外,在自定义搜索中也需要这样做bar class

In addition this is also required in the custom search bar class

-(void) setShowsScopeBar:(BOOL)showsScopeBar {
    [super setShowsScopeBar:YES]; //Initially make search bar appear with scope bar
}

这篇关于IOS7:uisearchdisplaycontroller始终显示范围栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆