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

标签 ios objective-c cgrectmake

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

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 个像素。我可能缺少偏移量吗?也许与导航栏有关?

最佳答案

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

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

“包裹”方式。它有点乱,但它可能会给你一些尝试的想法

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

关于ios - Objective-C - 在屏幕中间画一条水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003563/

相关文章:

ios - 如何将 UILabel 放置在 UIImageView 下方,使其始终均匀放置

ios - CoreLocation Swift 因后台信号 9 而终止

iphone - iOS 推特账户访问

objective-c - 如何向此 API 提交 JSON 数组?

iphone - 从 UITableView 更改我要推送到的 View 中的 UILabel

objective-c - 如何检索文本字段文本并将其作为整数 32 存储在核心数据中?

ios - Xcode 自动布局约束困惑

iOS 自定义字体移位

swift - 无法快速查看线路

xcode - 如何初始化相互依赖的属性