gpt4 book ai didi

ios - 透明的 UITableViewHeaderFooterView

转载 作者:技术小花猫 更新时间:2023-10-29 10:29:00 27 4
gpt4 key购买 nike

我正在尝试为这个 UITableView 创建一个自定义标题 View ,我希望它是透明的。

我的代码...

界面...

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 中,我正在加载 header ,例如...

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

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

我想要一个像标准 View 一样的标题 View ,您可以在其中看到在它后面滚动的表格 View 内容。

请告诉我该怎么做。

最佳答案

制作一个具有透明背景颜色的 View 并将其分配给 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)
}
}

关于ios - 透明的 UITableViewHeaderFooterView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19654473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com