UITableView background Image

前端 未结 12 1882
情歌与酒
情歌与酒 2021-01-29 21:21

I\'am trying to setup a png image as my tableview\'s background. With the following code all are fine! But only on iPhone Simulator. If I try to run the application on an iPhone

相关标签:
12条回答
  • 2021-01-29 22:08

    A little late but maybe for all the others looking for a solution. I could get that work by using the following code in the viewDidLoad method of the UITableViewController:

    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    self.tableView.backgroundColor = [UIColor clearColor];
    

    or with using just a background color:

    self.parentViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    

    It took me a while to figure that out but now it works like a charm.

    0 讨论(0)
  • 2021-01-29 22:10

    Please use following code.

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewBackground.png"]];
    [tempImageView setFrame:self.tableView.frame]; 
    
    self.tableView.backgroundView = tempImageView;
    [tempImageView release];
    

    Thanks,

    0 讨论(0)
  • 2021-01-29 22:12

    Simple swift version will be

    let tempImageView = UIImageView(image: UIImage(named: "yourImage"))
    tempImageView.frame = self.tableView.frame
    self.tableView.backgroundView = tempImageView;
    
    0 讨论(0)
  • 2021-01-29 22:12

    Thanks @Ravin for your answer.

    In Swift 5.0:

        let tempImageView = UIImageView(image: UIImage(named: "justin_bieber_topless.png"))
        tempImageView.frame = self.tableView.frame
        self.tableView.backgroundView = tempImageView;
    
    0 讨论(0)
  • 2021-01-29 22:14

    I think this approach cleaner:

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"TableBackground.jpg"]];
    self.tableView.backgroundColor = background;
    [background release];
    
    0 讨论(0)
  • 2021-01-29 22:20
    [TableView setBackgroundView:nil];
    [TableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.png"]] ];
    
    0 讨论(0)
提交回复
热议问题