【问题标题】:Draw top and bottom lines on a UIView在 UIView 上绘制顶线和底线
【发布时间】:2015-10-16 14:11:34
【问题描述】:

我正在尝试在UIView 的顶部和底部边缘画线。但是这条线不会一直绘制到视图控制器的右边缘。

以下是我用来绘制线条的代码:

- (void)addBorders
{
    CALayer *upperBorder = [CALayer layer];
    CALayer *bottomBorder = [CALayer layer];
    upperBorder.backgroundColor = [[UIColor colorWithRed:225/255.0 green:220/255.0 blue:214/255.0 alpha:1.0f] CGColor];
    upperBorder.frame = CGRectMake(0, 0, CGRectGetWidth(self.recentTuneinView.frame), 0.5f);
    bottomBorder.backgroundColor = [[UIColor colorWithRed:154/255.0 green:154/255.0 blue:154/255.0 alpha:1.0f] CGColor];
    bottomBorder.frame = CGRectMake(0, 58.0f, CGRectGetWidth(self.recentTuneinView.frame), 0.5f);
    [self.recentTuneinView.layer addSublayer:upperBorder];
    [self.recentTuneinView.layer addSublayer:bottomBorder];

}

这是显示问题的图像:

我在代码中遗漏了什么?

谢谢。

【问题讨论】:

  • 看到这个链接可能对你有帮助stackoverflow.com/questions/17355280/…
  • 不清楚你的方法何时被调用;绘图应该发生在内部,例如–drawInRect: 方法。
  • 它没有正确然后增加 cgrectframe 中的值
  • 方法在viewDidLoad中调用
  • @Anbu.Karthik :我从您提供的链接中获得了上述代码。谢谢

标签: ios objective-c uiview


【解决方案1】:

添加子层不是一个可扩展的解决方案,因为它会在旋转设备或更改视图大小时产生问题。

我的建议是创建一个自定义视图并实现 drawRect: 如下所示:

- (void)drawRect:(CGRect)iRect {
    CGContextRef aContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(aContext, 0.5);

    // Set Top Line Color
    CGContextSetStrokeColorWithColor(aContext, [[UIColor colorWithRed:225/255.0 green:220/255.0 blue:214/255.0 alpha:1.0f] CGColor]);

    // Top Line
    CGContextMoveToPoint(aContext, 0, 0);
    CGContextAddLineToPoint(aContext, iRect.size.width, 0);

    // Set Bottom Line Color
    CGContextSetStrokeColorWithColor(aContext, [[UIColor colorWithRed:225/255.0 green:220/255.0 blue:214/255.0 alpha:1.0f] CGColor]);

    // Bottom Line
    CGContextMoveToPoint(aContext, 0, 58.0);
    CGContextAddLineToPoint(aContext, iRect.size.width, 58.0);
}

【讨论】:

    【解决方案2】:

    这里是解决方案。以上代码已更新。

    - (void)addBorders
    {
        CALayer *upperBorder = [CALayer layer];
        CALayer *bottomBorder = [CALayer layer];
        upperBorder.backgroundColor = [[UIColor colorWithRed:225/255.0 green:220/255.0 blue:214/255.0 alpha:1.0f] CGColor];
        upperBorder.frame = CGRectMake(0, 0, CGRectGetWidth(self.recentTuneinView.frame) + 60, 0.5f);
        bottomBorder.backgroundColor = [[UIColor colorWithRed:154/255.0 green:154/255.0 blue:154/255.0 alpha:1.0f] CGColor];
        bottomBorder.frame = CGRectMake(0, 58.0f, CGRectGetWidth(self.recentTuneinView.frame) + 60, 0.5f);
        [self.recentTuneinView.layer addSublayer:upperBorder];
        [self.recentTuneinView.layer addSublayer:bottomBorder];
    
    }
    

    【讨论】:

      【解决方案3】:

      更新您的代码。

      CALayer *upperBorder = [CALayer layer];
      CALayer *rightBorder = [CALayer layer];
      CALayer *bottomBorder = [CALayer layer];
      upperBorder.backgroundColor = [[UIColor colorWithRed:225/255.0 green:220/255.0 blue:214/255.0 alpha:1.0f] CGColor];
      rightBorder.backgroundColor = upperBorder.backgroundColor;
      upperBorder.frame = CGRectMake(0, 0, CGRectGetWidth(recentTuneinView.frame), 0.5f);
      rightBorder.frame = CGRectMake(CGRectGetWidth(recentTuneinView.frame)-0.5, 0, 0.5f,CGRectGetHeight(recentTuneinView.frame));
      
      bottomBorder.backgroundColor = [[UIColor colorWithRed:154/255.0 green:154/255.0 blue:154/255.0 alpha:1.0f] CGColor];
      bottomBorder.frame = CGRectMake(0, 58.0f, CGRectGetWidth(recentTuneinView.frame), 0.5f);
      
      [recentTuneinView.layer addSublayer:upperBorder];
      [recentTuneinView.layer addSublayer:rightBorder];
      [recentTuneinView.layer addSublayer:bottomBorder];
      

      注意:您还必须添加正确的sublayer框架。

      【讨论】:

      • 我只想在顶部和底部添加线条。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多