2015-10-16 76 views
2

我正在试图在UIView的顶部和底部边缘上绘制线条。但是这条线不会一直画到视图控制器的右边。在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]; 

} 

这里是一个图像显示的问题:

enter image description here

什么我的代码所缺少?

谢谢。

+0

看到这个链接可能会帮助你http://stackoverflow.com/questions/17355280/how-to-add-a-border-just-on-the-top-side-of-a-uiview –

+0

它是您的方法被调用时不清楚;绘图应该发生在例如'-drawInRect:'方法。 – holex

+0

它没有使它正确,然后增加cgrectframe中的值 –

回答

2

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

我的建议是创建一个自定义视图和执行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); 
} 
+0

你是对的。我会尝试这个代码。谢谢 –

0

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

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

} 
+0

你测试了这个代码吗 – Jamil

+0

是的,我做过。它的工作 –

0

更新你的代码。

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

注意:你也必须添加正确的框架。

+0

我只想在顶部和底部添加线条。谢谢 –