简体   繁体   English

如何在使用iOS7的UINavigationBar时以编程方式定位UITableView?

[英]How do I position UITableView programmatically while using iOS7's UINavigationBar?

I have a quite regular UIViewController that is a part of a UINavigationController -hierarchy, which naturally makes the view have a UINavigationBar at the top. 我有一个非常规则的UIViewController ,它是UINavigationController -hierarchy的一部分,它自然地使视图在顶部有一个UINavigationBar As we all know, iOS7's navigation bars are very different from previous versions. 众所周知,iOS7的导航栏与以前的版本有很大不同。

If I drag a UITableView into my view in Storyboard, then the 'frame' for the table view is covering the entire view (IE [tableView setFrame:self.view.frame]; ). 如果我将UITableView拖到Storyboard中的视图中,那么表视图的“框架”将覆盖整个视图(IE [tableView setFrame:self.view.frame]; )。 Even behind the NavigationBar . 甚至在NavigationBar后面。 This makes so that if I scroll, the content will be faintly visible through the bar. 这使得如果我滚动,内容将通过栏微弱地看到。 Contrary to most people, I actually like this. 与大多数人相反,我其实喜欢这个。

In my current view controller, I would like to create the UITableView programmatically and place it as a subview here. 在我当前的视图控制器中,我想以编程方式创建UITableView并将其作为子视图放在此处。 However, I am unable to achieve the same effect this way. 但是,我无法通过这种方式达到同样的效果。

I have tried 我努力了

UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
[self.view addSubview: table];

This makes the 'top scrolling point' stay behind the navigation bar. 这使得“顶部滚动点”留在导航栏后面。 Imagine a single cell in a tableView, and it's faintly visible through the top bar. 想象一下tableView中的单个单元格,通过顶部条形图可以看到它。 If I scroll down, it pops right up behind it. 如果我向下滚动,它会在它后面弹出。 That's the default 'top position'. 这是默认的“顶级位置”。

I have also tried 我也试过了

CGRect frame = CGRectMake(0, 'nav.y+nav.height', self...width, self...height-y);    
UITableView *table = [[UITableView alloc] initWithFrame:frame];
[self.view addSubview: table];

to simply place the table view in the rect below the UINavigationBar , but then I won't get the scrolling transparency effect behind the bar. 简单地将表视图放在UINavigationBar下面的rect中,但是我不会在栏后面获得滚动透明效果。

I also tried the first example along with this: 我也尝试了第一个例子:

[tableView setContentOffset:CGPointMake(0, 'below navbar')];

which makes it stay exactly where I want it to stay, but as soon as I touch it (scroll and release), it scrolls back up behind the navigation bar, and stays there. 这使它保持在我希望它留下的位置,但是只要我触摸它(滚动和释放),它就会在导航栏后面向上滚动,并保持在那里。

What's the programmatic solution to achieve this effect? 实现这种效果的程序化解决方案是什么? Do I have to set the offset every time I scroll too far up, or is there a simpler solution? 每次滚动太远都需要设置偏移量,还是有更简单的解决方案? Along with this iOS7-style, I'd imagine they would add something like [tableView setVisibleFrame:] or something..? 除了这个iOS7风格,我想他们会添加像[tableView setVisibleFrame:]东西......?

Add the table in storyboard like normal but make sure you have it connected to an outlet (we will call it _tableView for now). 像往常一样在故事板中添加表格,但要确保将它连接到插座(我们现在称之为_tableView )。

Then in your view controller's -viewDidLoad set the top inset to be the status bar plus the navigation bar: 然后在视图控制器的-viewDidLoad中将顶部插图设置为状态栏和导航栏:

`self.tableView.contentInset = UIEdgeInsetsMake( (self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height), 0, 0, 0);' `self.tableView.contentInset = UIEdgeInsetsMake((self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height),0,0,0);'

This will make sure that all of the cells will be visible but will also fill the area behind the navigation bar with white so the bar doesn't end up gray. 这将确保所有单元格都可见,但也会用白色填充导航栏后面的区域,因此条形图不会灰色。

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

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