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

给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的背景色,效果不佳。每个cell中的背景是重复的!


正确的方式:在tableview后面放置一个背景视图,并将tableview设为透明色。

  1. UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];  
  2. backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]];  
  3. [window addSubview:backgroundView];  
  4.   
  5. yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped];  
  6. yourTableViewController.view.backgroundColor = [UIColor clearColor];  
  7. [window addSubview:yourTableViewController.view];  


这种方法产生的背景在整个表格Cell中是连续的整张图片。

当然,现在是直接在window下增加了背景层,有点不太灵活,在有导航栏的情况下,可以直接更改导航栏的背景,效果是一样的

  1. self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wood1.png"]];  


下面在单个的ViewController里更改背景


在用代码产生的tableViewcontroller中,可以通过loadview方法设置

  1. @interface SomeController : UIViewController <UITableViewDataSource, UITableViewDelegate> {  
  2.   ...  
  3.   UITableView *tableView;  
  4.   ...  
  5. }  
  6. @property (nonatomic, retain) UITableView *tableView;  
  7. @end  
  8.   
  9.   
  10.   
  11. @implementation SomeController  
  12.   
  13. @synthesize tableView;  
  14.   
  15. ...  
  16.   
  17. - (void)loadView {  
  18.     [super loadView];  
  19.     UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];  
  20.     [v setImage:[UIImage imageNamed:@"table_background.png"]];  
  21.     [self.view addSubview:v];  
  22.   
  23.   
  24.     self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];  
  25.     [self.tableView setBackgroundColor:[UIColor clearColor]];  
  26.     [self.view addSubview:self.tableView];  
  27. }  
  28. ...  
  29. @end 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值