iOS填坑之路ios相关点iOS开发技能集锦

UISearchController 爬坑!! 希望对你有用

2018-06-15  本文已影响2561人  我不相信me

 UISearchController iOS 11之后searchController有了新样式,它可以放在导航栏!!

好吧!我们就玩这样的样式,也只能这样了! 

/*************先讨论放在导航栏*************/

1 创建一个UITableViewController文件作为点击后搜索列表页面

2 ios11 后放到导航栏之前放在表头

  if(@available(iOS11.0, *)) {

        self.navigationItem.searchController = self.SearchController;

    }else    { 

self.TableView.tableHeaderView=self.SearchController.searchBar;   

}

3 初始化

-(UISearchController*)SearchController

{    if (!_SearchController) {

        //检索页

        TableViewController *tab =[[TableViewController alloc]init];

        //数据数组

        tab.dataArray =self.dataArray;

        //初始化

        _SearchController =[[UISearchController alloc]initWithSearchResultsController:tab];

//searchResultsUpdater代理放到检索页去实现

        _SearchController.searchResultsUpdater=tab;

//SearchController.delegate代理放到当前页处理

        _SearchController.delegate=self;

        //搜索时,背景色

        _SearchController.dimsBackgroundDuringPresentation=NO;

           [_SearchController.searchBar sizeToFit];

 self.definesPresentationContext = YES;

  }    return _SearchController;

}

问题1. 一开始没有显示呀 为何列表拖动才出现 !

是的默认就是这样的! 解决方法:     //直接显示 ios11还有一条属性设置  self.navigationItem.hidesSearchBarWhenScrolling=NO;   问题又来了列表滚动我想它滚动隐藏,没错你又得设置回来-(void)viewDidAppear:(BOOL)animated

{    if(@available(iOS11.0, *)) {

        //显示后设置回来滚动隐藏    

    self.navigationItem.hidesSearchBarWhenScrolling=YES;    }

}

问题2. 我点击UISearchController 消失了  什么鬼! (特殊情况)

网上答案都是这个 没有添加  self.definesPresentationContext = YES; 上移动64dp  问题是已经添加了!图层不见上移!

原因:当前视图不是父视图 当前很多设计页面都是放在在 ScrollView 上 然后[self addChildViewController:ViewController]; 所以不应该放在当前视图,(self.definesPresentationContext = YES;)放在它的父视图中!

问题3.searchBar样式怎么设置? 一顿猛如虎的简单设置之后你会发现很多没效果!  ios11 已经属于系统创建的图层!

特殊处理: 

设置searchBar样子和颜色  思路:添加一张你想要的一样样式的图片给它.

      //添加一张白色的图片(方法自己上网搜索)

      UIImage *image =[UIImage imageWithColor:[UIColor whiteColor]];

        //把白色的图片弄成自己想要的样子(图片处理大小和切圆角,方法自己上网搜索)

        image =[UIImage createRoundedRectImage:image size:CGSizeMake(self.view.frame.size.width, 34) radius:34/2];;

       [_SearchController.searchBar setSearchFieldBackgroundImage:image forState:UIControlStateNormal];

        //设置文字和放大镜的偏移

      [_SearchController.searchBar setSearchTextPositionAdjustment:UIOffsetMake(10, 0)];

设置 输入文字大小颜色 (控件位置 默认情况是靠上的)!!

经过研究给个一劳永逸的方法!!

viewWillLayoutSubviews:控制器的view将要布局子控件 时候设置

- (void)viewWillLayoutSubviews {

    [super  viewWillLayoutSubviews];

//获取TextField

   UITextField*searchField = [_SearchController.searchBar valueForKey:@"_searchField"];

//设置到中间(和放到表头一样/动画不好)

    searchField.center=_SearchController.searchBar.center;

//设置字号和颜色

    searchField.font = [UIFont systemFontOfSize:12];

    searchField.textColor=[UIColor redColor];

    [searchFieldsetValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];

    [searchFieldsetValue:[UIFont systemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];

}


最后取消按钮的设置  UISearchResultsUpdating 里面去处理(因为Button活动以后才有所以只能在检索时候设置)

- (void)updateSearchResultsForSearchController:(UISearchController*)searchController{

//这里是检索的

    self.navigationController.definesPresentationContext=YES;

    searchController.searchResultsController.view.hidden = NO;

    [searchController.searchBar setShowsCancelButton:YES animated:YES];

//kvo设置

    UIButton*cancelButton = [searchController.searchBarvalueForKey:@"_cancelButton"];

    //修改标题和标题颜色

   [cancelButtonsetTitle:@"取消" forState:UIControlStateNormal];

 cancelButton.titleLabel.font=[UIFont systemFontOfSize:12];

    //刷新表格  

//  [self.tableView reloadData];

}

/*************讨论放在表头************/ 

ios11之前就上面的代码直接运行

怎么做成ios11 一样呢! 先把颜色设置成导航颜色

_SearchController.searchBar.barTintColor=self.navigationController.navigationBar.barTintColor;

你会发现searchBar上下多了两条黑线!!!查看图层发现 一条是导航的黑线 一条是searchBar的黑线!

是的 我们就把这两条线也设置成这个颜色image

//(颜色转为image自己上网找)

[_SearchController.searchBar setBackgroundImage:[self  imageWithColor:self.navigationController.navigationBar.barTintColor]];

在方法下面区分下

  if(@available(iOS11.0, *)) {

        self.navigationItem.searchController = self.SearchController;

        //直接显示

        self.navigationItem.hidesSearchBarWhenScrolling=NO;

    }else

    {

        self.TableView.tableHeaderView=self.SearchController.searchBar;

        _SearchController.searchBar.barTintColor=self.navigationController.navigationBar.barTintColor;    

    [_SearchController.searchBar setBackgroundImage:[self  imageWithColor:self.navigationController.navigationBar.barTintColor]];

    }

最后就剩下导航线 要考虑到只能当前页面的样式,还有就是ios11之前才这样,所以两个条件

//视图将要显示时隐藏

-(void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

       if(@available(iOS11.0, *)) {

       }else

       {

           [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

           [self.navigationController.navigationBar setShadowImage:[UIImage new]];

       }

}

//视图将要消失时取消

-(void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

      if(@available(iOS11.0, *)) {

      }else

      {

          [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

          [self.navigationController.navigationBar setShadowImage:nil];

      }

}

好了基本一样了 对比下区别还是有一点点的 比较ios11 查看图层 searchBar 是顶置的 这样放在表头是居中的!!

!算了还是附赠方法吧免得到处找

+(UIImage*)imageWithColor:(UIColor*)color

{

    CGRectrect =CGRectMake(0.0f,0.0f,1.0f,1.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    returntheImage;

}

+(UIImage*)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)radius

{

    // the size of CGContextRef

    intw = size.width;

    inth = size.height;

    UIImage*img = image;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGRectrect =CGRectMake(0,0, w, h);

    CGContextBeginPath(context);

    addRoundedRectToPath(context, rect, radius, radius);

    CGContextClosePath(context);

    CGContextClip(context);

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    img = [UIImageimageWithCGImage:imageMasked];

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    CGImageRelease(imageMasked);

    return img;

}

static voidaddRoundedRectToPath(CGContextRefcontext,CGRectrect,floatovalWidth,

                                 floatovalHeight)

{

    floatfw, fh;

    if(ovalWidth ==0|| ovalHeight ==0)

    {

        CGContextAddRect(context, rect);

        return;

    }

    CGContextSaveGState(context);

    CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));

    CGContextScaleCTM(context, ovalWidth, ovalHeight);

    fw =CGRectGetWidth(rect) / ovalWidth;

    fh =CGRectGetHeight(rect) / ovalHeight;

    CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner

    CGContextAddArcToPoint(context, fw, fh, fw/2, fh,1);  // Top right corner

    CGContextAddArcToPoint(context,0, fh,0, fh/2,1);// Top left corner

    CGContextAddArcToPoint(context,0,0, fw/2,0,1);// Lower left corner

    CGContextAddArcToPoint(context, fw,0, fw, fh/2,1);// Back to lower right

    CGContextClosePath(context);

    CGContextRestoreGState(context);

}

上一篇 下一篇

猜你喜欢

热点阅读