[UISearchController自IOS 13以来状态栏中的UITableView意外绘制

问题描述 投票:0回答:1

我在一个非常基本的UINavigationBar中使用一个非常基本的UISearchController(但hidesNavigationBarDuringPresentation = YES)拥有一个非常基本的UITableController。我的问题是,如果搜索处于活动状态,则UITableView滚动在状态栏中可见。请注意,此行为是自IOS13以来的新行为,在此之前,由于不透明的搜索栏,这没有发生。对我来说,它看起来更像是一个IOS错误,而不是预期的行为。任何建议如何解决这个问题?理想情况下,我希望具有与IOS12及更高版本相同的外观。

IOS 13,滚动显示在状态栏中IOS 12,搜索栏不透明:没有问题“

#import <UIKit/UIKit.h>
@interface MyAppDelegate : UIResponder <UIApplicationDelegate,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>
{
  UIWindow* _win;
  UINavigationController* _nav;
  UITableViewController* _tc;
  NSMutableArray* _model;
  NSArray* _filteredModel;
  UISearchController* _search;
  UILabel* _label;
}
@end
@implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  _win=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  UIViewController* c=[[UIViewController alloc] init];
  UIView* v=c.view;
  v.backgroundColor=UIColor.whiteColor;
  _label=[[UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 20)];
  _model=[NSMutableArray array];
  _label.text=@"Tap on 'Show Table'";
  [v addSubview:_label];
  for (int i=0;i<200;i++) {
    _model[i]=[NSString stringWithFormat:@"Item %d",i];
  }
  _nav=[[UINavigationController alloc] initWithRootViewController:c];
  c.navigationItem.title=@"A title";
  UIBarButtonItem* b=[[UIBarButtonItem alloc] initWithTitle:@"Show Table" style:UIBarButtonItemStylePlain target:self action:@selector(showTable:)];
  c.navigationItem.rightBarButtonItem=b;
  _tc=[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
  _tc.definesPresentationContext=YES; //have to do it for other reasons
  _search=[[UISearchController alloc] initWithSearchResultsController:nil];
  _search.obscuresBackgroundDuringPresentation=NO; //needed to be able to tap in results
  _search.hidesNavigationBarDuringPresentation=YES; //have to do it for other reasons
  _search.searchResultsUpdater=self;
  UITableView* tv=_tc.tableView;
  tv.delegate=self;
  tv.dataSource=self;
  tv.tableHeaderView=_search.searchBar;
  _win.rootViewController=_nav;
  [_win makeKeyAndVisible];
  return YES;
}
-(void)showTable:(UIBarButtonItem*)b {
  [_nav pushViewController:_tc animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  _label.text=[NSString stringWithFormat:@"You selected row:%ld",(long)indexPath.row];
  [_nav popViewControllerAnimated:TRUE];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return _search.active?_filteredModel.count:_model.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString* reuse=@"whatever";
  UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:reuse];
  if (cell==nil) {
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
  }
  NSInteger row=indexPath.row;
  cell.textLabel.text=_search.active?_filteredModel[row]:_model[row];
  return cell;
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  NSString* searchText=searchController.searchBar.text;
  if (searchText.length==0) {
    _filteredModel=_model;
  } else {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                              @"SELF contains[c] %@",searchText];
    _filteredModel=[_model filteredArrayUsingPredicate:predicate];
  }
  [_tc.tableView reloadData];
}
@end
int main(int argc, char * argv[]) {
  @autoreleasepool {
      return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyAppDelegate class]));
  }
}

我在一个非常基本的UINavigationBar中使用一个非常基本的UISearchController有一个非常基本的UITableController(但hidesNavigationBarDuringPresentation = YES)。我的问题是,如果...

ios uisearchcontroller
1个回答
0
投票

此(错误)行为的原因是,在IOS13上,UISearchBarBackground视图不再覆盖状态栏。它确实在以前的IOS版本中进行了介绍。

IOS12 UISearchBarBackground覆盖整个上方区域>

IOS13 UISearchBarBackground不涵盖状态栏[我在代码中未涉及任何其他ViewController层次结构的解决方案是一个非常讨厌的黑客,将UISearchBarBackground的框架扩展为与IOS13之前版本中的状态匹配。

© www.soinside.com 2019 - 2024. All rights reserved.