2013-02-20 6 views
6

私はUITextViewのコーナーを、それを含むグループUITableViewCellの角に丸めてマスクしようとしています。それは現在、ここclipsToBoundsとmasksToBoundsグループ化されたUITableViewCell

UITextView inside grouped UITableViewCell

を立つ私はセルの境界線をオーバーラップからのコーナーを防ぐためにしようとするために使用しているコードの一部をだとして、ここでは、細胞のスクリーンショットです。私は

cell.contentView.layer.masksToBounds = YES; 
cell.layer.masksToBounds = YES; //tried this as a test, still doesn't work 
detailTextView.clipsToBounds = YES; 
[cell.contentView addSubview:detailTextView]; 

cell.layer.masksToBounds = YES; 
cell.contentView.layer.masksToBounds = YES; 
detailTextView.clipsToBounds = YES; 
[cell addSubview:detailTextView]; 

の両方を試してみました。これは明らかに私が何をしないのです、動作していませんか?

+0

あなたは 'cell.clipsToBounds = YES;'を試しましたか? – Patrick

+0

はい、それは私がとにかく必要なものではありません。 'clipsToBounds'は、セルがその層のうち親の外側にある部分を隠すことを意味します。 'UITextView'がその親の外側(セルcontentView /セル自体)のレイヤーの部分を隠すようにします – conorgriffin

+1

あなたのcontentViewの境界があなたのセルの境界よりも大きいのでcontentviewをクリッピングしても問題は解決しませんセルにクリップする必要があります – Pfitz

答えて

0

私はあなたがこのようにコーナーを隠すことはできないと確信しています。セルのbackgroundViewは、グループ化されたUITableViewのイメージですので、マスキングの感覚はありません。

問題を解決するには、角を丸くすることが考えられます。これは、上のセルの上隅と下のセルの下の角を丸めるだけなので、ややこしいです。幸いにも、@ lomanfはここに任意のコーナーを丸めるための素晴らしい解決策を投稿しました:Round two corners in UIView。彼のMTDContextCreateRoundedMaskメソッドを使用して、目標を達成できます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Other cell instantiation 

    // First cell in section 
    if (indexPath.row == 0) { 
     [self roundTopOrBottomCornersOfCell:cell top:YES];  
    } 
    // Last cell in section 
    else if (indexPath.row == tableView.numberOfRowsInSection:indexPath.section-1) { 
     [self roundTopOrBottomCornersOfCell:cell top:NO]; 
    } 
} 

// Modified from the second part of @lomanf's Solution 1 
- (void)roundTopOrBottomCornersOfCell:(UITableViewCell*)cell top:(BOOL)top { 
     // Set constant radius 
     CGFloat radius = 5.0; 

     // Create the mask image you need calling @lomanf's function 
     UIImage* mask; 
     if (top) { 
      mask = MTDContextCreateRoundedMask(self.view.bounds, radius, radius, 0.0, 0.0); 
     } 
     else { 
      mask = MTDContextCreateRoundedMask(self.view.bounds, 0.0, 0.0, radius, radius); 
     } 

     // Create a new layer that will work as a mask 
     CALayer* layerMask = [CALayer layer];    
     layerMask.frame = cell.bounds; 

     // Put the mask image as content of the layer 
     layerMask.contents = (id)mask.CGImage; 

     // Set the mask layer as mask of the view layer 
     cell.layer.mask = layerMask; 
}   
0

同じ問題は、違いは、私はプレーンスタイルを使用していると私は、丸みを帯びた角を持つすべてのセルを作るしようとしているということである私

に発生しました。最後に、私はそれを作った、ここに私の方法である:

1. は、カスタムtableViewCellのawakeFromNibメソッド

[cell.layer setMasksToBounds:YES]; 
    [cell.layer setCornerRadius:5.0]; 

2. がセット、白の色にカスタムTableViewCellのcontentviewの背景色を設定するには、このコードを配置あなたのtableViewの色をクリアするバックグラウンドの色。それで全部です。

関連する問題