UITableView 美化- 增加一个好看的背景
UITableView 美化- 增加一个好看的背景
给UITableView增加一个好看的背景能为应用程序增色不少,并能促进app的销售,但是随便增加一个背景图片会史你的app更加丑陋。
- //This method produces odd artifacts in the background image:
- ATableViewController *yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped];
- yourTableViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]];
正确的方式:在tableview后面放置一个背景视图,并将tableview设为透明色。
- UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];
- backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]];
- [window addSubview:backgroundView];
- yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped];
- yourTableViewController.view.backgroundColor = [UIColor clearColor];
- [window addSubview:yourTableViewController.view];
- - (void)loadView {
- [super loadView];
- UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
- [v setImage:[UIImage imageNamed:@"table_background.png"]];
- [self.view addSubview:v];
- self.tableView = [[[UIView alloc] initWithFrame:self.view.bounds] autorelease];
- [self.tableView setBackgroundColor:[UIColor clearColor]];
- [self.view addSubview:self.tableView];
- }
在用nib初始化的tableViewcontroller中,可以的在初始化实例前设置
- // create the view controller from your nib
- MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
- vc.tableView.backgroundColor = [UIColor clearColor];
- // create the background
- UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.png"]];
- iv.contentMode = UIViewContentModeCenter;
- iv.userInteractionEnabled = TRUE;
- [navigationController pushViewController:vc animated:YES];
- // put the background behind the tableview
- [vc.tableView.superview addSubview:iv];
- [iv addSubview:vc.tableView];
- // don't forget to release your view controller & image view!
- [vc release];
- [iv release];
评论暂时关闭