上下文
- 这是一个位于
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]
}
当UILabel
被添加到self.view
时
当UILabel
被添加到self.containerView
时
问题
- 为什么
UILabel
不位于屏幕顶部,甚至在状态栏后面? - 为什么
yPos
有区别,取决于是添加到self.view
还是self.containerView
最佳答案
更改标签的背景颜色,我想您会看到发生了什么。标签的高度为 100 像素,它在该空间内垂直居中。将高度更改为 20 或 30,然后重试。
关于ios - 为什么 Y 位置为 0 的 UILabel 不位于屏幕顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18345228/