Objective-C - 在屏幕中间画一条水平线

Objective-C - drawing a horizontal line through the middle of the screen

我正在尝试在具有导航栏的应用程序的屏幕中间绘制一条水平线。这是我当前的代码:

CGFloat screenWidth = self.view.frame.size.width;
CGFloat screenHeight = self.view.frame.size.height;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat lineThickness = 3;

UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectMake(0, (screenHeight - navigationBarHeight) / 2, screenWidth, lineThickness)];
[self.view addSubview:horizontalLine];

我使用非常相似的计算来绘制一条垂直线 - 效果很好。但是,问题是,无论使用何种设备,这条水平线在屏幕上的位置都太低了大约 10 个像素。我可能缺少偏移量吗?也许与导航栏有关?

好的,我先这样做了..但它没有居中。但它应该是! :) 在此之下,我将其更改为添加一个视图以将其全部包装起来,这似乎 "work" 您可以尝试其中一种或某些元素的组合,然后看看效果如何。

UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectZero];

[self.view addSubview:horizontalLine];

[horizontalLine setTranslatesAutoresizingMaskIntoConstraints: NO];

horizontalLine.backgroundColor = [UIColor blackColor];

id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;

NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(horizontalLine, topGuide, bottomGuide);

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[horizontalLine]|" options:0 metrics: 0 views:viewsDictionary]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topGuide][horizontalLine(==3)][bottomGuide]|" options:0 metrics: 0 views:viewsDictionary]];


[self.view addConstraint:
 [NSLayoutConstraint constraintWithItem:horizontalLine
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                                 toItem:self.view
                              attribute:NSLayoutAttributeCenterY
                             multiplier:1
                               constant:0]];

"wrapped"方法。它有点乱,但它可能会给你一些尝试的想法

UIView * wrapper = [[UIView alloc] initWithFrame:CGRectZero];

UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectZero];

[wrapper setTranslatesAutoresizingMaskIntoConstraints: NO];
[horizontalLine setTranslatesAutoresizingMaskIntoConstraints: NO];

[self.view addSubview:wrapper];
[wrapper addSubview:horizontalLine];

horizontalLine.backgroundColor = [UIColor blackColor];

id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;

NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(horizontalLine, topGuide, bottomGuide, wrapper);

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[wrapper]|" options:0 metrics: 0 views:viewsDictionary]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topGuide][wrapper][bottomGuide]|" options:0 metrics: 0 views:viewsDictionary]];

[wrapper addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[horizontalLine]|" options:0 metrics: 0 views:viewsDictionary]];

[wrapper addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=1)-[horizontalLine(==3)]-(>=1)-|" options:0 metrics: 0 views:viewsDictionary]];

[wrapper addConstraint:
 [NSLayoutConstraint constraintWithItem:horizontalLine
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                                 toItem:wrapper
                              attribute:NSLayoutAttributeCenterY
                             multiplier:1
                               constant:0]];

嗯,我喜欢@myte 的解决方案。如果我正在做同样的功能,我也会使用 NSLayoutConstraint。但是,要直接回答您的问题,您缺少的是状态栏。状态栏的高度通常为 20 像素。当您将其除以 2 时,您将得到 10 个像素。

CGFloat screenWidth = self.view.frame.size.width;
CGFloat screenHeight = self.view.frame.size.height;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat lineThickness = 3;

// This is what you are missing
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;

UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectMake(0, (screenHeight - navigationBarHeight - statusBarHeight) / 2, screenWidth, lineThickness)];
[self.view addSubview:horizontalLine];

P.S.: 你提到问题是"regardless of device",但是,如果你在横向模式下在iPhone 4S 上尝试,它可能只是每个孔都居中,因为状态栏被隐藏 :P (link)。