2015-10-16 111 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

我只想在頂部和底部添加線條。謝謝 –