2011-07-18 15 views
0

私のiPhoneアプリケーションでは、私のUITableViewのヘッダーを変更したいと思います。灰色のデフォルトのヘッダーと同じグラデーション/アルファ/ニースのスタイルを維持するにはどうすればいいですか?そして、例えば勾配 を描画するためにCoreGraphicsを使用します。UITableViewヘッダー - iPhone

+0

私は、これはあなたを助けることができると思う:動作しませんでした http://stackoverflow.com/questions/813068/uitableview-change-section-header-color – Peres

答えて

1

を使用することができます

- (void) drawRect:(CGRect)rect 
{ 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    CGGradientRef glossGradient; 
    CGColorSpaceRef rgbColorspace; 
    size_t num_locations = 2; 
    CGFloat locations[2] = { 0.0, 1 }; 

    const CGFloat *startColorComponents = CGColorGetComponents(startColor.CGColor); 
    const CGFloat *endColorComponents = CGColorGetComponents(endColor.CGColor); 

    CGFloat components[8] = { startColorComponents[0], startColorComponents[1], startColorComponents[2], startColorComponents[3], // Start color 
     endColorComponents[0], endColorComponents[1], endColorComponents[2], endColorComponents[3] }; // End color 

    rgbColorspace = CGColorSpaceCreateDeviceRGB(); 
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); 

    CGRect currentBounds = self.bounds; 
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); 
    CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)); 
    CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, bottomCenter, 0); 

    CGGradientRelease(glossGradient); 
    CGColorSpaceRelease(rgbColorspace); 
} 

次に、Sachinの提案に従いますが、代わりにカスタム表示クラスを使用してください。

+0

bottomCenterでCGRectGetMidY(currentBounds)が使用されている理由がわかりません。私はあなたが望んでいるのは実際には下のYであり、Yの半ばではないと思う。私はこれを全高に変更し、それはもっと良く働いた。それ以外はこれは素晴らしいです。 – clarky

0

あなたはグラデーションを適用したい場合は、カスタムビュークラスを作成し、その後のdrawRectをオーバーライドすることができ、この

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero]; 
    headerView.backgroundColor = [UIColor lightGrayColor]; 
    return headerView; 

    } 
+0

。それはtableViewヘッダーの見栄えを削除し、タイトルが削除されました – CodeGuy

+0

フレームが設定されていないので、動作しません。 –

関連する問題