我正在尝试在具有导航栏的应用程序的屏幕中间绘制一条水平线。这是我当前的代码:
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/