透明的UITableViewHeaderFooterView

时间:2013-10-29 09:37:43

标签: ios objective-c uitableview

我正在尝试为此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;
}

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

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

请你告诉我怎么做。

13 个答案:

答案 0 :(得分: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;
}

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

答案 1 :(得分:10)

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

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)
    }
}

答案 2 :(得分:9)

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

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

答案 3 :(得分:7)

有一种方法可以让你仍然可以使用你的自定义UITableViewHeaderFooterView并获得一个清晰的背景,无需任何" hack"而不只是在滚动表格来刷新标题时。

只需添加一个背景清晰的自定义背景视图:)就像那样简单(在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;
}

答案 4 :(得分:6)

更新:显然,我先前建议的答案在iOS 13+中不再起作用,这就是我在控制台中得到的-

在UITableViewHeaderFooterView上设置背景色已 不推荐使用。请使用所需背景设置自定义UIView 颜色改为backgroundView属性。

进一步,查看一些评论here,将tintColor设置为.clear可以解决问题。

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

有趣的编码:)

答案 5 :(得分:5)

好的,来自Twitter上的@schukin。

我将MyHeaderView更改为子类UIView,然后设置...

self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];

然后在代表......

- (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;
}

现在这完全符合我的要求。

似乎UITableViewHeaderFooterView不能做我正在寻找的事情。

谢谢大家。

答案 6 :(得分: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;
}

答案 7 :(得分: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

答案 8 :(得分:0)

-(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;
}

答案 9 :(得分:0)

其他人提供的解决方案已被弃用。这是我从其他答案中得到的信息。

“不推荐在UITableViewHeaderFooterView上设置背景颜色。请改用contentView.backgroundColor。”

我的解决方案是针对以下内容。希望它可以帮助某人。 : - )

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   UILabel *label = [[UILabel alloc] init];

   label.text = sectionNames[section];
   label.textAlignment = NSTextAlignmentCenter;
   label.textColor = [UIColor blackColor];
   label.backgroundColor = [UIColor clearColor];

   // Creates a thin border around the section header 
   label.layer.borderColor = [UIColor blackColor].CGColor;
   label.layer.borderWidth = 1;
   label.layer.cornerRadius = 16;

   return label; }

答案 10 :(得分:0)

最简单的解决方案是设置cell.backgroundColorcell.contentView.backgroundColor

的backgroundColor

Swift 2。*: self.backgroundView?.backgroundColor = UIColor.clearColor() self.contentView.backgroundColor = UIColor.clearColor()

注意: 别忘了textLabels的backgroundColor!

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

答案 11 :(得分:0)

如果您只是对部分之间的空格使用footerview但不希望它拥抱底部,这是一个更简单的解决方案,也不会弄乱已弃用的footerView,因此您不会获得这些警告。

隐藏它。

    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(CellID.Footer.rawValue)
    footerView?.hidden = true
    return footerView
}

答案 12 :(得分:-1)

检查Apple开发人员文档后,作为描述

  

@interface UITableViewHeaderFooterView:UIView

     

UITableViewHeaderFooterView类实现了一个可重用的视图   可以放在表格部分的顶部或底部。你使用标题   和页脚显示该部分的其他信息。您   在大多数情况下,可以在没有子类化的情况下使用此类。如果你   要显示自定义内容,请为您的内容创建子视图   并将它们添加到contentView属性中的视图。你也可以   为backgroundView属性分配一个可选的背景视图。

我试过这种方法,它对我来说很完美

self.tintColor = [UIColor whiteColor];
UIView *backView = [[UIView alloc] initWithFrame:self.bounds];
backView.backgroundColor = [UIColor whiteColor];
self.backgroundView = backView;
相关问题