歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> UITableView 美化- 增加一個好看的背景

UITableView 美化- 增加一個好看的背景

日期:2017/3/1 10:29:02   编辑:Linux編程

給UITableView增加一個好看的背景能為應用程序增色不少,並能促進app的銷售,但是隨便增加一個背景圖片會史你的app更加丑陋。

  1. //This method produces odd artifacts in the background image:
  2. ATableViewController *yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped];
  3. yourTableViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]];
這種方法直接設置tableview的背景色,效果不佳。

正確的方式:在tableview後面放置一個背景視圖,並將tableview設為透明色。

  1. UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];
  2. backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]];
  3. [window addSubview:backgroundView];
  4. yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped];
  5. yourTableViewController.view.backgroundColor = [UIColor clearColor];
  6. [window addSubview:yourTableViewController.view];
在用代碼產生的tableViewcontroller中,可以通過loadview方法設置
  1. - (void)loadView {
  2. [super loadView];
  3. UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
  4. [v setImage:[UIImage imageNamed:@"table_background.png"]];
  5. [self.view addSubview:v];
  6. self.tableView = [[[UIView alloc] initWithFrame:self.view.bounds] autorelease];
  7. [self.tableView setBackgroundColor:[UIColor clearColor]];
  8. [self.view addSubview:self.tableView];
  9. }

在用nib初始化的tableViewcontroller中,可以的在初始化實例前設置

  1. // create the view controller from your nib
  2. MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
  3. vc.tableView.backgroundColor = [UIColor clearColor];
  4. // create the background
  5. UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.png"]];
  6. iv.contentMode = UIViewContentModeCenter;
  7. iv.userInteractionEnabled = TRUE;
  8. [navigationController pushViewController:vc animated:YES];
  9. // put the background behind the tableview
  10. [vc.tableView.superview addSubview:iv];
  11. [iv addSubview:vc.tableView];
  12. // don't forget to release your view controller & image view!
  13. [vc release];
  14. [iv release];
Copyright © Linux教程網 All Rights Reserved