如何修复表格视图搜索栏与状态栏重叠问题

5
在导航控制器中有一个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

这是结果:

enter image description here

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


最佳解决方案是创建一个新的UIView,从顶部留下20像素,并在该HeaderView中添加UISearchBar,然后将整个视图分配为TableHeaderView,您的问题将得到解决。 - Ruchish Shah
1
我发现你没有将UITableViewController嵌入到UINavigationController中。从Storyboard中,你只需要选择视图控制器并转到“Editor”>“嵌入”>“导航控制器”即可实现。 - agy
它已经在导航控制器中了,我可以从故事板文件清晰地看到:https://onedrive.live.com/redir?resid=768cf89e7cf13325!4438&authkey=!AKP0Wbsec7-Zo-g&v=3&ithint=photo%2cpng - Ramy Al Zuhouri
4个回答

7
我认为你需要这段代码。
在你的viewDidLoad方法中添加以下代码:
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

您的表格视图将如下所示:

enter image description here

编辑:

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

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

这样,当我启动应用程序时,我仍然会看到表视图和状态栏重叠在一起,但如果我滚动它,它就会被修复。请观看此视频:https://onedrive.live.com/redir?resid=768cf89e7cf13325!4440&authkey=!ANbvmk8nT8NLO5I&ithint=video%2cmov - Ramy Al Zuhouri
1
@RamyAlZuhouri 请尝试添加tableView.scrollToRowAtIndexPath(NSIndexPath(index: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)。 - Leo Dabus

1
我了解您正在使用Swift编写代码,但这是如何在Objective-C中隐藏状态栏的方法。
- (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 iOS应用程序中如何隐藏状态栏的答案(点击此处查看)


0
尝试将以下代码放在AppDelegate类的didFinishLaunching中。
在Swift中 -
    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类中添加以下方法 -
Objective-C -
-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
 } 

Swift-

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

它有点能用,但现在状态栏变成了一个黑色的间隙:https://onedrive.live.com/redir?resid=768cf89e7cf13325!4439&authkey=!AI6RsI-Nc4DSBwY&v=3&ithint=photo%2cpng - Ramy Al Zuhouri
嗨@RamyAlZuhouri,请尝试编辑后的代码。它一定会起作用的。 - NehaK

0

添加 UISearchControllerDelegate;

然后

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接