2

I am trying to draw lines on the top and bottom edge of a UIView. But the line does not get drawn all the way to the right edge of the view controller.

Following is the code I am using to draw the lines:

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

}

Here is an image showing the problem:

enter image description here

What am I missing in the code?

Thanks.

s.d.
  • 363
  • 3
  • 14

3 Answers3

3

Adding sub layers is not a scalable solution like it creates problems when rotating the device or view size change.

My advise is to create a custom view and implement drawRect: something like this:

- (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);
}
Abhinav
  • 37,684
  • 43
  • 191
  • 309
0

Here is the solution. Above code updated.

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

}
s.d.
  • 363
  • 3
  • 14
0

Update your code.

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

Note: You have to add right sublayer frame also.

Jamil
  • 2,977
  • 1
  • 13
  • 23