为啥imageView的底部和UILabel中的第一个UITableViewCell之间有空格?

Posted

技术标签:

【中文标题】为啥imageView的底部和UILabel中的第一个UITableViewCell之间有空格?【英文标题】:Why is there space between the bottom of the imageView and the first TableViewCell in the UITable?为什么imageView的底部和UILabel中的第一个UITableViewCell之间有空格? 【发布时间】:2013-12-07 02:35:31 【问题描述】:

我有一个视图控制器,它在 viewDidLoad 中设置 UIImageView 和 UITableView,如下所示:

// Root UIView
UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
rootView.backgroundColor = [UIColor redColor];
self.view = rootView;

// Image View
NSString *path = [[NSBundle mainBundle] pathForResource:@"test320x180" ofType:@"JPG"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
self.imageView.image = image;
[self.view addSubview:self.imageView];

// Table View
self.tableView = [[UITableView alloc] initWithFrame:rootView.frame style:UITableViewStylePlain];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.contentInset = UIEdgeInsetsMake(image.size.height, 0, 0, 0);
self.tableView.contentOffset = CGPointMake(0, -image.size.height);
self.tableView.backgroundColor = [UIColor clearColor]; // so we can see the image view
[self.tableView reloadData];
[self.view addSubview:self.tableView];

图像视图底部有 20 个点,它有 CGRect: (0, 0, 320, 180) 和 Table View 的第一个单元格,其原点是 (0, 200),在下面的屏幕截图。

在显示中,我看到 TableView 在 ImageView 下方 20 点处开始,截图如下。我最好的猜测是表格视图会自动考虑状态栏,但图像视图不会。

我的目的是让图像和第一个 tableview 单元格刷新,但我不确定如何在不向我的代码中添加幻数 20 的情况下保证这一点。

【问题讨论】:

您使用什么工具以这种方式(红色背景)和 3d 视图(很像 firefoxes webdev 工具之一)显示/“显示”xib? 其实叫Reveal。 【参考方案1】:

[[UIScreen mainScreen] applicationFrame] 返回一个包含整个屏幕高度的值,包括状态栏的高度,并且总是在屏幕坐标中

例如,在 3.5 英寸 iPhone 上,无论是纵向还是横向,您都会获得相同的应用程序框架:

 0,0, 320, 480  // iPhone 3.5-inch, applicationFrame in both landscape and portrait

当您将 origin.y 为 0 的框架分配给图像视图时,然后通过以下方式添加它:

[self.view addSubview:self.imageView];

...然后self.imageView的前20个像素隐藏在状态栏下方。

请注意,有时状态栏的高度会加倍,例如通过电话、在应用内录制音频或个人热点/网络共享。为了生存下去,您需要以下值:

CGSize size = [[UIApplication sharedApplication] statusBarFrame].size;

这是在屏幕坐标中返回的,所以返回的矩形可能是:

 0,0, 320, 20  // iPhone 3.5-inch, portrait
 0,0, 20, 480  // iPhone 3.5-inch, landscape
 0,0, 320, 40  // iPhone 3.5-inch, portrait when tethering or other phone call
 0,0, 40, 480  // iPhone

因此,快速的解决方案是使用类似上述 CGRect 大小代码的代码,如下所示:

CGFloat height = (size.width < size.height? size.width : size.height);

3.5 英寸,网络共享或其他电话时横向

    使用 CGRect 设置 rootView 框架的大小 针对状态栏高度进行了调整,所以 imageView.frame.origin.y == 0 是 不被状态栏隐藏 将imageView的框架原点y设置为状态栏高度(这意味着rootView的内容仍然与状态栏重叠)。

【讨论】:

谢谢。在我初始化 rootView 之后,我做了rootView.frame = CGRectOffset(rootView.frame, 0, -[[UIApplication sharedApplication] statusBarFrame].size.height);,这似乎已经修复了它。 作为补充,您可以通过 self.edgesForExtendedLayout = UIRectEdgeNone; 禁用 ios 7 扩展边缘插入或取消选择情节提要中所需 ViewController 上的“扩展边缘...”。对此***.com/questions/18900428/… 进行了长时间的讨论 @JuJoDi 酷。我还建议将 self 作为观察者添加到 NSNotificationCenter 以获取 UIApplicationWillChangeStatusBarFrameNotification 和 UIApplicationDidChangeStatusBarFrameNotification。查看developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/… 谢谢,那肯定会进入最终版本。【参考方案2】:

UITableView 是 UIScrollView 的子类,因此请检查 contentInset 属性并自动设置 UIViewController 属性,AdjustsScrollViewInsets 设置为 NO。

我认为 automaticAdjustsScrollViewInsets 将 insets 应用于所有滚动视图以说明状态栏、按钮栏和导航栏。

【讨论】:

【参考方案3】:

当我在情节提要中工作时,我遇到了同样的问题,我的 tableview 具有相同的 20 像素间隙。所以你需要做的是:

1.) 选择您的视图控制器并在属性检查器中取消选择“调整滚动视图插图”。 (这将消除差距 - 表格视图假定导航栏)

2.) 选择您的 tableView 并在尺寸检查器中设置“节标题高度”== '1'(这将消除 20 像素间隙 - 该 tableview 假定为状态栏)

希望能帮到你! 谢谢

【讨论】:

以上是关于为啥imageView的底部和UILabel中的第一个UITableViewCell之间有空格?的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UILabel 文本顶部和底部空间居中

ImageView 在 UITableViewCell 中坚持使用 UILabel

具有 layout_height="wrap_content" 的 android XML 布局中的 ImageView 具有顶部和底部填充

在不停止 stackView 自动调整大小的情况下对齐图像视图

uni-app为啥image底部填充不满以及解决办法

iOS 约束:如何给 UILabel 高度约束优先级