透明的UITableViewHeaderFooterView

Fog*_*ter 12 objective-c uitableview ios

我正在尝试为此创建一个自定义标题视图UITableView,我希望它是透明的.

我的代码......

接口...

typedef void(^ActionBlock)();

@interface MyViewHeaderView : UITableViewHeaderFooterView

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

@property (nonatomic, copy) ActionBlock tapAction;

@end
Run Code Online (Sandbox Code Playgroud)

实现...

#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
Run Code Online (Sandbox Code Playgroud)

在我的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;
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,标题显示正常.只是没有透明度.

我希望有一个标题视图的标题视图,您可以在其中看到滚动的表视图内容.

请你告诉我怎么做.

小智 16

请原谅我无法使用stackoverflow ....

以下是我使用这两种UITableviewDelegate方法实现它的方法:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
  if ([view isMemberOfClass:[UITableViewHeaderFooterView class]]) {
    ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor clearColor];
  }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UITableViewHeaderFooterView *feedHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderIdentifier"];

  //Other customizations

  return feedHeaderView;
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助,让我永远想弄清楚.似乎在init中没有创建backgroundView,因此无法在那里覆盖颜色.


Sat*_*ito 11

创建一个具有透明背景颜色的视图,并将其指定给UITableViewHeaderFooterView的backgroundView属性.

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)
    }
}
Run Code Online (Sandbox Code Playgroud)


ima*_*nae 10

更新:显然,我之前建议的答案在 iOS 13+ 中不再有效,这是我在控制台中得到的 -

不推荐在 UITableViewHeaderFooterView 上设置背景颜色。请改为将具有所需背景颜色的自定义 UIView 设置为 backgroundView 属性。

此外,查看这里的一些评论,设置tintColor.clear成功。

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = .clear
}
Run Code Online (Sandbox Code Playgroud)

玩得开心编码:)


Erw*_*wan 9

在iOS 7上,如果您只想在默认的部分标题视图中强制使用透明背景,那么您可以执行以下操作:

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    [[(UITableViewHeaderFooterView*)view backgroundView] setBackgroundColor:[UIColor clearColor]];
}
Run Code Online (Sandbox Code Playgroud)


Nic*_*zzi 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是最简单的解决方案.实际上,甚至不需要设置清晰的颜色或框架尺寸.只是设置一个新的UIView工作正常.self.backgroundView = [UIView new]; (4认同)

Fog*_*ter 5

好的,来自Twitter上的@schukin.

我改为MyHeaderView子类UIView而是设置...

self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];
Run Code Online (Sandbox Code Playgroud)

然后在代表......

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MyViewHeaderView *headerView = [[MyHeaderView alloc] initWithFrame:<a frame>];

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

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

    return headerView;
}
Run Code Online (Sandbox Code Playgroud)

这现在完全按照我的意愿运作.

似乎UITableViewHeaderFooterView不能做我正在寻找的东西.

谢谢大家.


小智 5

最简单的方法是:

- (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;
}
Run Code Online (Sandbox Code Playgroud)