LoginSignup
23
24

More than 5 years have passed since last update.

[Swift] TableView で背景画像を表示させる

Posted at

iOS幼稚園年少さんのメモです。

TableViewに背景を設置

UITableViewControllerのサブクラスのViewDidLoadにて。

class FooTableView: UITableViewController {
  override func viewDidLoad() {
   ・
   ・
    // make UIImageView instance
    var imageView = UIImageView(frame: CGRectMake(0, 0, self.tableView.frame.width, self.tableView.frame.height))
    // read image
    let image = UIImage(named: "imageFileName")
    // set image to ImageView
    imageView.image = image
    // set alpha value of imageView
    imageView.alpha = 0.5
    // set imageView to backgroundView of TableView
    self.tableView.backgroundView = imageView
   ・
   ・
  }
}

とりあえずこれで背景画像は設置されます。
しかし、これだけだとcell群の後ろに隠れたままです。

Cellを透過させる

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  ・
  ・      
  var cell = tableView.dequeueReusableCellWithIdentifier("fooCell", forIndexPath: indexPath) as! FooTableViewCell // これはCustomCellを使ってる場合
  ・
  ・
  // cellの背景を透過
  cell.backgroundColor = UIColor.clearColor()
  // cell内のcontentViewの背景を透過
  cell.contentView.backgroundColor = UIColor.clearColor()
  ・
  ・
  return cell
}

これでTableViewの背景が表示されるかと思います。

References

23
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
24