繁体   English   中英

如何修复表视图的搜索栏与状态栏重叠

[英]How to fix table view's search bar overlapping with status bar

我在导航控制器中有一个UITableViewController,带有一个搜索栏。 这是我在viewDidLoad中添加搜索栏的方法:

let resultsController = SearchTableViewController()
resultsController.people = people
searchController = UISearchController(searchResultsController: resultsController)
let searchBar = searchController.searchBar
searchBar.placeholder = "Search a person"
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController.searchResultsUpdater = resultsController

这是结果:

在此输入图像描述

我尝试在故事板中编辑表格视图以添加约束以使其从顶视图的边距更远,但我无法添加约束,可能是因为表视图位于UITableViewController中。

我想你需要这个代码。

viewDidLoad方法中添加以下代码:

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

你的tableview将是这样的:

在此输入图像描述

编辑:

您可以使用以下代码强制滚动表:

tableView.scrollToRowAtIndexPath( NSIndexPath(index: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)

我知道你在swift中编写代码,但这就是你在objectiveC中隐藏状态栏的方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        // iOS 7
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    } else {
        // iOS 6
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }
}

// Add this Method
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

这是swift中的答案如何在Swift iOS应用程序中隐藏状态栏?

尝试将以下代码放在AppDelegate类的didFinishLaunching

在迅速 -

    var version = ( UIDevice.currentDevice().systemVersion as NSString ).floatValue

   if (version >= 7) {
        application.setStatusBarStyle(  UIStatusBarStyle.LightContent, animated: true);
        window?.clipsToBounds = true;
        window?.frame =  CGRectMake(0,20,self.window!.frame.size.width,self.window!.frame.size.height-20);
    }

在Objective-c中 -

    if (UIDevice.currentDevice.systemVersion] floatValue] >= 7) {
        [application setStatusBarStyle:UIStatusBarStyleLightContent];
        self.window.clipsToBounds =YES;
        self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
    }

并在ViewController类中添加以下方法 -

目的-C-

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
 } 

迅速-

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}

添加UISearchControllerDelegate;

然后

-(void)willDismissSearchController:(UISearchController *)searchController{
_tableView.contentInset = UIEdgeInsetsMake(20, 0, 44, 0);}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM