30
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UITableViewCellの選択色をStoryboard上で設定する

Last updated at Posted at 2015-01-31

UITableViewCellの選択色って何気にStoryboard上では設定できませんよね。Side-Menu.iOSというOSSのソースを読んでいてうまいやり方だなと思ったので紹介します。

ソースコード

まず、UITableViewCellのextensionを作ります。そこで新しい
@IBInspectableUIColorのプロパティを宣言して、セッターでselectedBackgroundViewを書き換えてます。

import UIKit

extension UITableViewCell {

    @IBInspectable
    var selectedBackgroundColor: UIColor? {
        get {
            return selectedBackgroundView?.backgroundColor
        }
        set(color) {
            let background = UIView()
            background.backgroundColor = color
            selectedBackgroundView = background
        }
    }

}

Storyboard上

Storyboard上では、こんな風にいつものカラーピッカーで変えるだけです。

スクリーンショット 2015-02-01 0.45.24.png

結果

スクリーンショット 2015-02-01 0.47.38.png

30
26
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
30
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?