Add UIView behind UITableView in UITableViewController code

前端 未结 3 547
慢半拍i
慢半拍i 2021-01-04 03:39

I would like to use a fixed image as the background in a simple grouped table view in my iPhone program. Unfortunately, no matter what I do, the background is always solid

相关标签:
3条回答
  • 2021-01-04 03:53

    Use UITableView's subviews property to add your background there:

    [self.tableView addSubview:backgroundView];
    [self.tableView sendSubviewToBack:backgroundView];
    

    Also, your cell/header etc. will probably need to be set to transparent for your backgroundView to be visible.

    0 讨论(0)
  • 2021-01-04 04:04

    As of iOS 3.2 the UITableView has a property called backgroundView. So instead of adding any subviews, just do this:

    self.tableView.backgroundView = backgroundView;
    

    The table view will take care of resizing the background view to the size of the table. The background view will not move with the table cells. In other words, if the background view is an image view, the image will appear to stay locked on the screen with the cells gliding across it. The default table views also seem to have the smarts to make the appropriate elements of the cell clear automatically if there is a background view present.

    0 讨论(0)
  • 2021-01-04 04:10

    I would simply set the backgroundColor property of the UITableView itself. You can use UIColor's +colorWithPatternImage: method to convert a UIImage into a UIColor (that will repeat across the entire view, if not big enough):

    // From within UITableViewController's -viewDidLoad:
    UIImage *image = [UIImage imageNamed:"yourImage.png"];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:image];

    Simply add yourImage.png and yourImage@2x.png to your application bundle, size it appropriately, and you're all set.

    0 讨论(0)
提交回复
热议问题