【问题标题】:Objective-C - drawing a horizontal line through the middle of the screenObjective-C - 在屏幕中间画一条水平线
【发布时间】:2015-01-17 19:48:11
【问题描述】:

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

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

【问题讨论】:

  • 你是说状态栏高度?
  • 我的意思是使用导航控制器的导航栏

标签: ios objective-c cgrectmake


【解决方案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]];

【讨论】:

    【解决方案2】:

    嗯,我确实喜欢@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.:您提到问题是“与设备无关”,但是,如果您在 iPhone 4S 上以横向模式尝试它,它可能只是居中,因为状态栏是隐藏的 :P (link) .

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      相关资源
      最近更新 更多