ios - 为什么 Y 位置为 0 的 UILabel 不位于屏幕顶部

标签 ios uiview uilabel

上下文

  • 这是一个位于 UINavigationController 堆栈中的 UIViewController
  • 在这个 UIViewController 中,我以编程方式在 (0,0) 的 (x,y) 坐标处添加了一个 UILabel
  • 我尝试过将 UILabel 添加到 self.view(这是在 UIViewController 中)或将 UILabel 添加到 UIView,这 UIView 是 self.containerView

self.containerView 通过这段代码创建并添加到 View 中:

- (void)addContainerView
{
    // Create a UIView with same frame as the screen bounds
    CGRect containerViewFrame = [[UIScreen mainScreen] applicationFrame];
    self.containerView = [[UIView alloc] initWithFrame:containerViewFrame];

    // Give the UIView a red background
    self.containerView.backgroundColor = [UIColor redColor];

    // Add the view
    [self.view addSubview:self.containerView];
}

UILabel 是通过这段代码添加的:

- (void)addTestLabel
{
    self.navigationController.navigationBarHidden = YES;
    CGRect frame = CGRectMake(0, 0, 100, 100);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = @"this is a test";
    [self.view addSubview:label]; // OR [self.containerView addSubview:label]
}

enter image description here

UILabel被添加到self.view

enter image description here

UILabel被添加到self.containerView

问题

  • 为什么 UILabel 不位于屏幕顶部,甚至在状态栏后面?
  • 为什么yPos有区别,取决于是添加到self.view还是self.containerView

最佳答案

更改标签的背景颜色,我想您会看到发生了什么。标签的高度为 100 像素,它在该空间内垂直居中。将高度更改为 20 或 30,然后重试。

关于ios - 为什么 Y 位置为 0 的 UILabel 不位于屏幕顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18345228/

相关文章:

objective-c - 带有 UILabel subview 缓存问题的 UITableViewCell

ios - 转换子图层时如何防止调用 layoutSubviews?

ios - UIScrollView内的UILabel上的触摸检测

ios - 架构 armv7 的 undefined symbol : "_OBJC_CLASS_$_MPMusicPlayerController"

ios - 切换显示/隐藏子表格单元格 iOS

iphone - 如何在 pickerview 上添加完成按钮?

objective-c - UIView touchesbegan 在动画期间没有响应

ios - 添加 subview 并以编程方式使用约束定位它

android - React Native - 在相机中叠加

iphone - 在iPhone中如何根据用户角色显示不同的选项卡