iphone - 如何在 UITableView 中制作固定背景 View ?

标签 iphone uitableview view background scroll

我正在尝试在分组的 UITableView 后面添加一个 View 。不幸的是,每当我滚动时,背景 View 也会移动。即使我使用 [self.view insertSubview:backgroundView belowSubview:_tableView] 或 [_tableView setBackgroundView:backgroundView] 也会发生这种情况。为什么这个背景 View 会滚动?另外,为什么我的 tableView 会滚动,即使我已禁用滚动?这些有关系吗?

在应用程序中委托(delegate):

    History *historyController = [[History alloc] init];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:historyController];

历史.m:

- (void)viewDidLoad {

    CGRect frame = self.view.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setScrollEnabled:NO];
    [_tableView setBackgroundColor:[UIColor clearColor]];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    UIView *bg = [[UIView alloc] initWithFrame:frame];
    [bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CenterFade.png"]];
    iv.alpha = .750;
    [bg addSubview:iv];
    [iv release]; iv = nil;

    [self.view addSubview:bg];
    [self.view addSubview:_tableView];
    [bg release]; bg = nil;
    [_tableView release];

    [super viewDidLoad];
}

最佳答案

尝试

- (void)viewDidLoad {

    CGRect frame = self.view.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;

    UIView *bg = [[UIView alloc] initWithFrame:frame];
    [bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CenterFade.png"]];
    iv.alpha = .750;
    [bg addSubview:iv];
    [iv release];
    iv = nil;

    _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setScrollEnabled:NO];
    [_tableView setBackgroundColor:[UIColor clearColor]];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [bg addSubview:_tableView];
    [_tableView release];
    _tableView = nil;
    [self.view addSubview:bg];
    [bg release];
    bg = nil;

    [super viewDidLoad];
}

如果您的 Controller 是UITableViewController只需将其更改为 UIViewController<UITableViewDelegate, UITableViewDatasource>tableView属性(相同)

关于iphone - 如何在 UITableView 中制作固定背景 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8402284/

相关文章:

ios - 将 DATE 添加到 Xcode xcconfig 文件

iphone - 我们如何在iPhone中进行后台文件上传?

mysql group by,两个条件,限制1

iphone - UINavigationController:推送的 View 何时收到 Dealloc 消息?

ios - 单点触控按钮中的事件处理程序问题

ios - 通过 segue 传递数据

xcode - iOS 8 Xcode6 heightForRowAtIndexPath 调用了额外的时间

ios - 在 Swift 中从 viewController 的新实例调用时,performSegueWithIdentifier 不起作用

Android:将一张图片放在另一张图片上

iphone - 几秒钟后用改变颜色填充圆圈