2016-03-31 81 views
0

我是iOS新手。我正在做一個聊天應用程序。下面的圖片是表格消息。在每一個細胞,我設置4個角落messageView,並顯示正確的UIBezierPath的角落爲靈活的視圖(視圖可以通過內容增加寬度高度)

self.messageView.layer.cornerRadius = 10.0; 

enter image description here

現在,我想我的一些消息只有3角(左上,左下,右下)和一些消息有3個角落(左下角,右下角,右上角) - 像facebook messenger
所以我用戶UIBezierPath爲每個單元格。
但我的消息不顯示完整的內容。 但是,當我向上/向下滾動時,單元格重新創建並顯示正確。

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.messageView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)]; 

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.frame = self.bounds; 
maskLayer.path = maskPath.CGPath; 
self.messageView.layer.mask = maskLayer; 

我想我的問題是: 當我爲我的self.messageView設置康納斯通過UIBezierPath,該self.messageView沒有完成畫呢。所以self.messageView.bounds返回錯誤的值。
但我不知道如何解決它。

任何幫助將不勝感激

+0

你試過'sizeToFit'嗎? – schmidt9

+0

@ schmidt9請引導我在哪裏我應該把'sizeToFit'? –

+0

請閱讀https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/sizeToFit – schmidt9

回答

1

它似乎並不要更新的-layoutSubviews方法的人。
添加的圖層不知道如何處理您的視圖,因此您需要在正在應用它的視圖的layoutSubviews中更改它們的大小。
保持對掩碼路徑的引用並更新它,在此之前始終調用super。

+0

謝謝。我解決它,通過更新'layoutSubView'內的角落 –

1

試試這個!

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.messageView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)]; 

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    // maskLayer.frame = self.bounds; 

    maskLayer.frame = self.messageView.bounds; // Try this. 
    maskLayer.path = maskPath.CGPath; 
    self.messageView.layer.mask = maskLayer; 

enter image description here enter image description here

+0

謝謝。我試過了,但同樣的問題 –

+0

我試過了。它工作正常..添加此。 [self setNeedsDisplay]; –

+0

我的問題是在tableview單元格中,並且單元格可以增加高度。在你的圖像中,你的視圖高度是固定的 –

相關問題