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


正確的方式:在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];


這種方法產生的背景在整個表格Cell中是連續的整張圖片。

當然,現在是直接在window下增加了背景層,有點不太靈活,在有導航欄的情況下,可以直接更改導航欄的背景,效果是一樣的

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


下面在單個的ViewController裏更改背景


在用代碼產生的tableViewcontroller中,可以通過loadview方法設置

@interface SomeController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
  ...
  UITableView *tableView;
  ...
}
@property (nonatomic, retain) UITableView *tableView;
@end



@implementation SomeController

@synthesize tableView;

...

- (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 = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.tableView];
}
...
@end


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章