TableView で背景画像を表示させる
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の背景が表示されるかと思います。