iOS 问题总结(五)

1.在 swift 工程中 使用 cocoapods 导入第三方库后出现 no such module afnetworking 错误

解决办法:
这是swift项目,在Podfile文件中加入“use_frameworks!” 然后pod install一下,回到项目中Command+b,pod install后,它还是源码状态,所以需要build一下。

2. 使用 cocoapods 时,编译报错

在 Building Setting 中的Other Linker Flags 中检查是不是为空了,如果是那么添加一句 $(inherited),再重新编译就不会报错了。

3.使用 UISearchController 遇到的问题:

我自定义了一个 HeaderView,如下图:

iOS 问题总结(五)_第1张图片
HeaderView

然后在创建 tableView 的时候,设置了 tableHeaderView,然后把 searchController 添加到了 headerView 上,如下代码:

YMCustomerHeader *headerView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([YMCustomerHeader class]) owner:nil options:nil] lastObject];
[headerView.segmentedControl addTarget:self action:@selector(segmentedControlClick:) forControlEvents:UIControlEventValueChanged];
self.searchController.searchBar.x = 0;
self.searchController.searchBar.y = 0;
[headerView addSubview:self.searchController.searchBar];
tableView.tableHeaderView = headerView;

下面是设置 searchController 的代码:

-(UISearchController *)searchController {
    if (_searchController == nil) {
        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        _searchController.searchBar.backgroundImage = [UIImage imageNamed:@"manage_customer_archive_search_background"];
        _searchController.searchResultsUpdater = self;
        _searchController.dimsBackgroundDuringPresentation = NO;
        _searchController.searchBar.placeholder = @"搜索";
        [_searchController.searchBar sizeToFit];
    }
    return _searchController;
}

运行后发现搜索栏的位置偏移了 -64 的高度,导致不能在屏幕上显示,如下图:

iOS 问题总结(五)_第2张图片

这时需要添加一行代码:

_searchController.hidesNavigationBarDuringPresentation = YES;

这行代码是声明,哪个viewcontroller显示UISearchController,苹果开发中心的demo中的对这行代码,注释如下

 // know where you want UISearchController to be displayed

如果不添加上面这行代码,在设置 hidesNavigationBarDuringPresentation 这个属性为YES的时候,搜索框进入编辑模式会导致,搜索栏不可见,偏移 -64 ;在设置为 NO 的时候,进入编辑模式输入内容会导致高度为 64 的白条,猜测是导航栏没有渲染出来。

但是经过测试,情况还是和上图一样,搜索栏还是偏移 -64,不能显示。然后我又添加了下面的代码:

_searchController.hidesNavigationBarDuringPresentation = NO;

运行如下图:

iOS 问题总结(五)_第3张图片

现在搜索栏没有发生偏移,但是导航栏却没有隐藏,于是我把 NO 改为了 YES,运行如下图:

iOS 问题总结(五)_第4张图片
iOS 问题总结(五)_第5张图片

结果还是发生了偏移。

然后我发现 definesPresentationContextUIViewController 的一个属性。所以我就把上面的代码改成了:

self.definesPresentationContext = YES;

运行如下图:

iOS 问题总结(五)_第6张图片
iOS 问题总结(五)_第7张图片

What the fuck?

然后我又把隐藏导航栏设置为了YES

_searchController.hidesNavigationBarDuringPresentation = YES;

搜索框就能正常显示了,如下图:

iOS 问题总结(五)_第8张图片

然后我又试着把隐藏导航栏的属性注释掉,然后运行,还是能够正常显示,下面是随后的代码:

-(UISearchController *)searchController {
    if (_searchController == nil) {
        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        _searchController.searchBar.backgroundImage = [UIImage imageNamed:@"manage_customer_archive_search_background"];
        _searchController.searchResultsUpdater = self;
        _searchController.dimsBackgroundDuringPresentation = NO;
        _searchController.definesPresentationContext = YES;
        self.definesPresentationContext = YES;
        _searchController.searchBar.placeholder = @"搜索";
        [_searchController.searchBar sizeToFit];
    }
    return _searchController;
}

到此搜索框就能正常是显示了。

你可能感兴趣的:(iOS 问题总结(五))