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
- 在这个
中,我以编程方式在(x,y)坐标(0,0)处添加一个UIViewController
UILabel
- 我已经尝试将
添加到self.view(这在UIViewController中)或将UILabel
添加到UILabel
,这UIView
是self.containerViewUIView
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)。如果降低标签的高度,可以看到差异。或者简单地为标签设置背景色。您可以看到标签的框架。