国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

UISearchDisplayController陰影遮蓋frame調整

2019-11-14 20:36:02
字體:
來源:轉載
供稿:網友

iOS7中UISearchDisplayController 與UISearchBar結合使用時,有時候會出現搜索框獲得焦點時,陰影遮蓋部分擋住了搜索框,影響用戶使用,如下圖

 

API中沒有陰影圖層的接口,嘗試分析解決

1、使用Reveal,查找遮蓋圖層,發現為_UISearchDisplayControllerDimmingView

2、找到該圖層,修改對應的frame,通過上圖可以發現dimmingview與searchResultsTableView為同一視圖的子圖層。

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
   for(UIView * v in controller.searchResultsTableView.superview.subviews)    {        NSLog(@"%@",[v class]);        if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])        {            v.frame = CGRectMake(0,20,320,400); //        }    }

}

 3、通過以上代碼修改,遮罩圖層沒有在擋住搜索框了,但操作搜索框,還是發現搜索框的下區域不能獲取焦點,Cancel按鈕不方便使用。

4、通過上個圖可以看到雖然遮罩層下去了,但還是有個圖層在擋住,左邊列表該圖層顯示為searchResultsTableView的父視圖,再次修改代碼。

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{        controller.searchResultsTableView.superview.bounds = CGRectMake(0,22,320,400);    for(UIView * v in controller.searchResultsTableView.superview.subviews)    {        NSLog(@"%@",[v class]);        if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])        {            v.frame = CGRectMake(0,20,320,400); //        }    }}

 5、搜索框可以正常使用。

6、同樣,如果需要調整searchResultsTableView的frame,在追加下面的代碼

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView  {    tableView.frame =CGRectMake(0, 20, 320, 480-64-44);}

 7、多次測試后發現,每次第一次執行無效,原因是由于searchDisplayControllerWillBeginSearch第一次執行時searchResultsTableView.superview為null,沒有添加到父視圖,可以使用兩種方案解決

方案一,延時執行:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{    NSLog(@"%@,%@",self.searchDisplayController.searchResultsTableView,self.searchDisplayController.searchResultsTableView.superview);        [self performSelector:@selector(resetFrame) withObject:nil afterDelay:0.1];}- (void)resetFrame{    CGRect bounds =  self.searchDisplayController.searchResultsTableView.superview.bounds;    CGFloat offset = CGRectGetMinY(bounds);    if (offset == 0)    {        self.searchDisplayController.searchResultsTableView.superview.bounds =CGRectMake(0,20,320,400);    }    for(UIView * v in self.searchDisplayController.searchResultsTableView.superview.subviews)    {        NSLog(@"%@",[v class]);        if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])        {            v.frame = CGRectMake(0,20,320,400); //        }    }}

方案二,注冊鍵盤通知:

- (void)viewDidAppear:(BOOL)animated {    [super viewDidAppear:animated];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetFrame)                                                 name:UIKeyboardWillShowNotification object:nil];}- (void)viewDidDisappear:(BOOL)animated {    [super viewDidDisappear:animated];        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];}- (void)resetFrame{    CGRect bounds =  self.searchDisplayController.searchResultsTableView.superview.bounds;    CGFloat offset = CGRectGetMinY(bounds);    if (offset == 0)    {        self.searchDisplayController.searchResultsTableView.superview.bounds =CGRectMake(0,20,320,400);    }    for(UIView * v in self.searchDisplayController.searchResultsTableView.superview.subviews)    {        NSLog(@"%@",[v class]);        if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])        {            v.frame = CGRectMake(0,20,320,400); //        }    }}

 

問題解決!

注:以上frame調整在ios7執行,ios7之前版本不需要,具體調整的frame大小可以根據自己的需求修改。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 密云县| 虎林市| 永靖县| 社会| 石屏县| 瑞安市| 湟源县| 祥云县| 祁连县| 子洲县| 石景山区| 屯昌县| 晋宁县| 昭觉县| 海丰县| 天镇县| 乌兰县| 清新县| 赫章县| 安福县| 桃园市| 磴口县| 商城县| 万山特区| 财经| 吉木萨尔县| 岳阳县| 宜君县| 宝坻区| 蓬安县| 衢州市| 互助| 罗定市| 金坛市| 高碑店市| 商洛市| 辰溪县| 西青区| 中西区| 赞皇县| 望谟县|