UISearchController 爬坑!! 希望對(duì)你有用!!

?UISearchController?iOS 11之后searchController有了新樣式,它可以放在導(dǎo)航欄!!

好吧!我們就玩這樣的樣式,也只能這樣了!?

/*************先討論放在導(dǎo)航欄*************/

1 創(chuàng)建一個(gè)UITableViewController文件作為點(diǎn)擊后搜索列表頁(yè)面

2 ios11 后放到導(dǎo)航欄之前放在表頭

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

? ? ? ? self.navigationItem.searchController = self.SearchController;

? ? }else ? ?{?

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

}

3 初始化

-(UISearchController*)SearchController

{ ? ?if (!_SearchController) {

? ? ? ? //檢索頁(yè)

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

? ? ? ? //數(shù)據(jù)數(shù)組

? ? ? ? tab.dataArray =self.dataArray;

? ? ? ? //初始化

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

//searchResultsUpdater代理放到檢索頁(yè)去實(shí)現(xiàn)

? ? ? ? _SearchController.searchResultsUpdater=tab;

//SearchController.delegate代理放到當(dāng)前頁(yè)處理

? ? ? ? _SearchController.delegate=self;

? ? ? ? //搜索時(shí),背景色

? ? ? ? _SearchController.dimsBackgroundDuringPresentation=NO;

?? ? ? ? ? [_SearchController.searchBar sizeToFit];

?self.definesPresentationContext = YES;

? } ? ?return _SearchController;

}

問(wèn)題1. 一開(kāi)始沒(méi)有顯示呀 為何列表拖動(dòng)才出現(xiàn) !


是的默認(rèn)就是這樣的! 解決方法:?? ? //直接顯示 ios11還有一條屬性設(shè)置 ?self.navigationItem.hidesSearchBarWhenScrolling=NO; ? 問(wèn)題又來(lái)了列表滾動(dòng)我想它滾動(dòng)隱藏,沒(méi)錯(cuò)你又得設(shè)置回來(lái)-(void)viewDidAppear:(BOOL)animated

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

? ? ? ? //顯示后設(shè)置回來(lái)滾動(dòng)隱藏 ? ?

?? ?self.navigationItem.hidesSearchBarWhenScrolling=YES; ? ?}

}

問(wèn)題2. 我點(diǎn)擊UISearchController?消失了 ?什么鬼! (特殊情況)

網(wǎng)上答案都是這個(gè) 沒(méi)有添加??self.definesPresentationContext = YES; 上移動(dòng)64dp ?問(wèn)題是已經(jīng)添加了!圖層不見(jiàn)上移!

原因:當(dāng)前視圖不是父視圖 當(dāng)前很多設(shè)計(jì)頁(yè)面都是放在在 ScrollView?上 然后[self addChildViewController:ViewController]; 所以不應(yīng)該放在當(dāng)前視圖,(self.definesPresentationContext = YES;)放在它的父視圖中!


問(wèn)題3.searchBar樣式怎么設(shè)置? 一頓猛如虎的簡(jiǎn)單設(shè)置之后你會(huì)發(fā)現(xiàn)很多沒(méi)效果! ?ios11 已經(jīng)屬于系統(tǒng)創(chuàng)建的圖層!

特殊處理:?

設(shè)置searchBar樣子和顏色 ?思路:添加一張你想要的一樣樣式的圖片給它.

? ? ? //添加一張白色的圖片(方法自己上網(wǎng)搜索)

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

? ? ? ? //把白色的圖片弄成自己想要的樣子(圖片處理大小和切圓角,方法自己上網(wǎng)搜索)

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

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

? ? ? ? //設(shè)置文字和放大鏡的偏移

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


設(shè)置 輸入文字大小顏色 (控件位置 默認(rèn)情況是靠上的)!!

經(jīng)過(guò)研究給個(gè)一勞永逸的方法!!

viewWillLayoutSubviews:控制器的view將要布局子控件 時(shí)候設(shè)置

- (void)viewWillLayoutSubviews {

? ? [super? viewWillLayoutSubviews];

//獲取TextField

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

//設(shè)置到中間(和放到表頭一樣/動(dòng)畫不好)

? ? searchField.center=_SearchController.searchBar.center;

//設(shè)置字號(hào)和顏色

? ? searchField.font = [UIFont systemFontOfSize:12];

? ? searchField.textColor=[UIColor redColor];

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

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

}


最后取消按鈕的設(shè)置 ?UISearchResultsUpdating 里面去處理(因?yàn)锽utton活動(dòng)以后才有所以只能在檢索時(shí)候設(shè)置)

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

//這里是檢索的

?? ?self.navigationController.definesPresentationContext=YES;

? ? searchController.searchResultsController.view.hidden = NO;

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

//kvo設(shè)置

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

? ? //修改標(biāo)題和標(biāo)題顏色

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

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

? ? //刷新表格 ?

// ?[self.tableView reloadData];

}


/*************討論放在表頭************/?

ios11之前就上面的代碼直接運(yùn)行


怎么做成ios11 一樣呢! 先把顏色設(shè)置成導(dǎo)航顏色

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

你會(huì)發(fā)現(xiàn)searchBar上下多了兩條黑線!!!查看圖層發(fā)現(xiàn) 一條是導(dǎo)航的黑線 一條是searchBar的黑線!


是的 我們就把這兩條線也設(shè)置成這個(gè)顏色image

//(顏色轉(zhuǎn)為image自己上網(wǎng)找)

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


在方法下面區(qū)分下

? 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]];

? ? }

最后就剩下導(dǎo)航線 要考慮到只能當(dāng)前頁(yè)面的樣式,還有就是ios11之前才這樣,所以兩個(gè)條件

//視圖將要顯示時(shí)隱藏

-(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]];

?? ? ? }

}

//視圖將要消失時(shí)取消

-(void)viewWillDisappear:(BOOL)animated

{

? ? [super viewWillDisappear:animated];

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

? ? ? }else

? ? ? {

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

? ? ? ? ? [self.navigationController.navigationBar setShadowImage:nil];

? ? ? }

}


好了基本一樣了 對(duì)比下區(qū)別還是有一點(diǎn)點(diǎn)的 比較ios11?查看圖層?searchBar 是頂置的 這樣放在表頭是居中的!!

!算了還是附贈(zèng)方法吧免得到處找

+(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);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容