ios UITableView + 在顶部添加内容偏移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2339620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:58:42  来源:igfitidea点击:

UITableView + Add content offset at top

iosuitableviewcocoa-touchuikitcontentoffset

提问by bluefloyd8

I need to add some blank space to the top of my UITableView that does not affect the size of the content area. Shifting the content down or adding a blank cell is NOT what I want to do. Instead I just want an offset.

我需要在 UITableView 顶部添加一些不影响内容区域大小的空白区域。向下移动内容或添加一个空白单元格不是我想要做的。相反,我只想要一个偏移量。

How?

如何?

回答by jigzat

I'm not sure if I'm following you but I think I'm having the same predicament. In my case I must give some space to the ADBannerView at the top of the screen so what I did was in the viewDidLoad method I added:

我不确定我是否在关注您,但我想我遇到了同样的困境。在我的情况下,我必须给屏幕顶部的 ADBannerView 一些空间,所以我所做的是在我添加的 viewDidLoad 方法中:

[self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];

the values it takes are UIEdgeInsetsMake(top,left,bottom,right).

它采用的值是 UIEdgeInsetsMake(top,left,bottom,right)。

Alternatively the same with Swift:

或者与 Swift 相同:

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

Swift 4.2:

斯威夫特 4.2:

self.tableView.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)

回答by scottbates22

You can add an "empty" header view to the table... this would give the initial appearance of the table to have an offset, but once you started scrolling the offset would be gone. NOt sure that's what you want.

您可以在“空”头视图添加到表...这将给该表的初始外观有偏移,但一旦你开始滚动的偏移量也就不复存在了。不确定那是你想要的。

If you need a permanent offset and are not already using section headers, then you could create the offset similarly to above by making custom views for the section headers, especially if you just have one section, this could give you the look of a permanent offset.

如果您需要一个永久偏移并且尚未使用节标题,那么您可以通过为节标题制作自定义视图来创建与上述类似的偏移,特别是如果您只有一个节,这可能会给您一个永久偏移的外观.

I can post sample code if it sounds like either of those are what you are looking for.

如果听起来您正在寻找其中任何一个,我可以发布示例代码。

回答by richy

I combined Jigzat's answer with:

我将 Jigzat 的回答与:

[self.tableView scrollRectToVisible:CGRectMake(0, 0, 320, 1) animated:NO];

in

- (void)viewDidLoad

so the first cell isn't at the top.

所以第一个单元格不在顶部。

回答by Rom4in

swift 5.1

迅捷 5.1

tableView.contentInset.top = 100

回答by willcodejavaforfood

Sounds like you want to wrapa 'View' around your UITableView. If you have a UITableViewControllerin IB the UITableViewwill automatically be set to the viewof UITableViewController. You change viewproperty to a normal UIViewand add your UITableViewin there and give it a offset.

听起来你想在你的UITableView. 如果你有一个UITableViewController在IB的UITableView将被自动设置到viewUITableViewController。您将view属性更改为法线UIView并将您的属性添加UITableView到其中并给它一个偏移量。

---Edit--- I just read my post and thought it made little sense :) When you create a UITableViewControlleryou get this (in pseudo code):

---编辑---我刚读了我的帖子,认为这没什么意义:)当你创建一个时,UITableViewController你会得到这个(伪代码):

UITableViewController.view = UITableView

This means that the actual table will take up the whole space and you cannot even add other views. So you need to change the

这意味着实际的表将占据整个空间,您甚至无法添加其他视图。所以你需要改变

UITableViewController.view = UIView

and add your table to that UIView

并将您的表格添加到该表格中 UIView

回答by TheJeff

I combined this answer with this one: https://stackoverflow.com/a/9450345/1993937

我将这个答案与这个答案结合起来:https: //stackoverflow.com/a/9450345/1993937

To make the tableView appear at the top of the content inset, so the space at the top isn't cut off by having the tableView scrolled down slightly when the view initially appears. (18 is my top gap)

为了使 tableView 出现在内容插入的顶部,因此当视图最初出现时,通过使 tableView 稍微向下滚动不会切断顶部的空间。(18 是我最大的差距)

[self.tableView setContentInset:UIEdgeInsetsMake(18,0,0,0)];
[self.tableView setContentOffset:
 CGPointMake(0, -self.songListTable.contentInset.top) animated:YES];