20

このためのカスタム ヘッダー ビューを作成しようとしていUITableViewますが、透明にしたいと考えています。

私のコード...

インターフェース...

typedef void(^ActionBlock)();

@interface MyViewHeaderView : UITableViewHeaderFooterView

@property (nonatomic, strong) UIImageView *flagImageView;
@property (nonatomic, strong) UILabel *leagueNameLabel;

@property (nonatomic, copy) ActionBlock tapAction;

@end

実装...

#import "MyViewHeaderView.h"

@interface MyViewHeaderView ()

@end

@implementation MyViewHeaderView

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        // Add customisation here...

        // I have tried everything here...
        self.tintColor = [UIColor colorWithWhite:0.961 alpha:1.0];
        self.alpha = 0.5;

        // self.contentView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        // self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        // can't remember what else.
        // none of it makes it transparent. It sets the colour against
        // a white background. i.e. 50% transparent but with a white opaque background.
        // so I can't see the content of the table scrolling behind it.

        self.flagImageView = [[UIImageView alloc] init];
        self.flagImageView.image = [UIImage imageNamed:@"placeholder_flag"];
        [self.flagImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.contentView addSubview:self.flagImageView];

        self.leagueNameLabel = [[UILabel alloc] init];
        [self.leagueNameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.contentView addSubview:self.leagueNameLabel];

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped)];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        tapGestureRecognizer.numberOfTouchesRequired = 1;
        [self.contentView addGestureRecognizer:tapGestureRecognizer];

        [self setupConstraints];
    }
    return self;
}

- (void)setupConstraints
{
    // adding constraints...
}

- (void)viewTapped
{
    self.tapAction();
}

@end

私のUITableViewDelegate中では、ヘッダーを次のようにロードしています...

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MyViewHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderView"];

    League *league = self.leagues[(NSUInteger) section];

    headerView.leagueNameLabel.text = league.name;
    headerView.tapAction = ^(){
        [self leagueTapped:league];
    };

    return headerView;
}

これは正常に機能しており、ヘッダーは適切に表示されています。透明感がないだけ。

テーブルビューのコンテンツがその後ろにスクロールするのを見ることができる標準ビューのようなヘッダービューが欲しいです。

これを行う方法を教えてください。

4

13 に答える 13

20

Make a view which has transparent background color and assign it to the UITableViewHeaderFooterView's backgroundView property.

class HeaderView: UITableViewHeaderFooterView{

    override
    init(reuseIdentifier: String?){
        super.init(reuseIdentifier: reuseIdentifier)
        self.backgroundView = UIView()
        self.backgroundView!.backgroundColor = UIColor.clearColor()
    }

    required
    init(coder: NSCoder){
        super.init(coder: coder)
    }

    override
    init(frame: CGRect){
        super.init(frame: frame)
    }
}
于 2014-12-02T01:28:49.047 に答える
9

iOS 7 で、デフォルトのセクション ヘッダー ビューの透明な背景のみを強制したい場合は、次のようにします。

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    [[(UITableViewHeaderFooterView*)view backgroundView] setBackgroundColor:[UIColor clearColor]];
}
于 2014-04-08T07:38:56.480 に答える
7

テーブルをスクロールしてヘッダーを更新するときだけでなく、カスタム UITableViewHeaderFooterView を引き続き使用して、「ハック」なしで常に機能する明確な背景を取得する方法があります。

明確な背景を持つカスタム背景ビューを追加するだけです:)それと同じくらい簡単です(iOS 7.1でテスト済み)

私のはこれです

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"MessagesChatGroupSection"
                                                         owner:self
                                                       options:nil];
        UIView *nibView = [objects firstObject];
        self.frame = nibView.bounds;
        self.contentView.frame = nibView.bounds;
        [self.contentView addSubview:nibView];
        self.backgroundView = [[UIView alloc] initWithFrame:nibView.bounds];
        self.backgroundView.backgroundColor = [UIColor clearColor];
    }

    return self;
}
于 2014-04-14T08:50:33.883 に答える
6

最も簡単な方法は次のとおりです。

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundView = [UIView new]; // removes system background view
    }
    return self;
}
于 2014-10-29T18:16:49.493 に答える
1
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *cellIdentifier = @"HeaderCell";

    UITableViewHeaderFooterView *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];

    if (nil == cell)
    {
        cell = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:cellIdentifier];
        cell.backgroundView = [[UIImageView alloc] init]; //<--- why not?
    }
    return cell;
}
于 2014-02-18T14:08:59.523 に答える
0

cell.backgroundColor最も簡単な解決策は、との backgroundColor を設定することです。cell.contentView.backgroundColor

スウィフト 2.*: self.backgroundView?.backgroundColor = UIColor.clearColor() self.contentView.backgroundColor = UIColor.clearColor()

注: textLabels の backgroundColor を忘れないでください。

cell.textLabel?.backgroundColor = UIColor.clearColor()

于 2016-03-11T16:17:33.233 に答える
0

多分それは助けます...

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *customTitleView = [[ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 32)] autorelease];
    UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(19, 0, 300, 32)];
    titleLabel.textColor = [UIColor darkGrayColor];
    titleLabel.shadowColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    [titleLabel setFont:[UIFont fontWithName: @"Helvetica-Bold" size: 17.0]];

    switch (section)
    {
        case 0:
            titleLabel.text = @"Title for section...";

            break;

        default:
            break;
    }

    [customTitleView addSubview:titleLabel];
    [titleLabel release];
    return customTitleView;

}

フッター(別のフレームとフォントサイズ)の場合...同じコードを追加します

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
于 2013-10-29T10:44:33.177 に答える