我试图在具有导航栏的应用程序的屏幕中间画一条水平线。这是我目前的代码:
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像素。我可能会错过一个偏移吗?可能与导航栏有关吗?
答案 0 :(得分:1)
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]];
答案 1 :(得分: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];
PS:你提到问题是"无论设备是什么"但是,如果你在横向模式的iPhone 4S上试用它,它可能只是居中,因为状态栏被隐藏:P (link)。