Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么Y位置为0的UILabel不位于屏幕顶部_Ios_Uiview_Uilabel - Fatal编程技术网

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

Ios 为什么Y位置为0的UILabel不位于屏幕顶部,ios,uiview,uilabel,Ios,Uiview,Uilabel,上下文 这是一个UIViewController,它位于UINavigationController堆栈中 在这个UIViewController中,我以编程方式在(x,y)坐标(0,0)处添加一个UILabel 我已经尝试将UILabel添加到self.view(这在UIViewController中)或将UILabel添加到UIView,这UIView是self.containerView self.containerView通过以下代码创建并添加到视图中: - (void)addCon

上下文

  • 这是一个
    UIViewController
    ,它位于
    UINavigationController
    堆栈中
  • 在这个
    UIViewController
    中,我以编程方式在(x,y)坐标(0,0)处添加一个
    UILabel
  • 我已经尝试将
    UILabel
    添加到self.view(这在UIViewController中)或将
    UILabel
    添加到
    UIView
    ,这
    UIView
    是self.containerView
self.containerView
通过以下代码创建并添加到视图中:

- (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];
}
- (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]
}
ui标签
通过以下代码添加:

- (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];
}
- (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]
}

ui标签添加到
self.view

UILabel
添加到
self.containerView

问题

  • 为什么
    ui标签不在屏幕顶部,甚至在状态栏后面
  • 为什么
    ypo
    之间存在差异,这取决于它是添加到
    self.view
    还是
    self.containerView

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

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

这是一个针对iOS 7的问题。由于iOS 7属于NDA,请用iOS 6演示此问题(我不相信您会看到完全相同的问题),或在开发论坛上提问。self.view或self.containerView的
y=0
位置与视图相对,而不是屏幕。@RobNapier我在iOS6中构建了它,并且能够复制。我认为主要问题是标签的高度。虽然我仍然不能100%确定self.containerView和self之间的区别。view@drc:这是由于标签的高度。标签的原点仍然是(0,0)。如果降低标签的高度,可以看到差异。或者简单地为标签设置背景色。您可以看到标签的框架。这是一个针对iOS 7的问题。由于iOS 7属于NDA,请用iOS 6演示此问题(我不相信您会看到完全相同的问题),或在开发论坛上提问。self.view或self.containerView的
y=0
位置与视图相对,而不是屏幕。@RobNapier我在iOS6中构建了它,并且能够复制。我认为主要问题是标签的高度。虽然我仍然不能100%确定self.containerView和self之间的区别。view@drc:这是由于标签的高度。标签的原点仍然是(0,0)。如果降低标签的高度,可以看到差异。或者简单地为标签设置背景色。您可以看到标签的框架。