LoginSignup
5
6

More than 5 years have passed since last update.

[tvOS]ホーム画面にシェルフを表示する

Posted at

シェルフ

棚、という意味

シェルフの表示条件

一番上の段にアイコンが置いてあることが条件となる。

Simulator Screen Shot 2015.10.26 20.51.38.png

一番上にアイコンがある例

Simulator Screen Shot 2015.10.26 20.51.25.png

一番上段にアイコンが無いと、シェルフは表示されない。

シェルフの立ち位置

Keyboard Extensionのようにエクステンションの扱いになる。

プロジェクトに追加する

スクリーンショット 2015-10-26 20.55.06.png
NewからTargetを選ぶ

スクリーンショット 2015-10-26 20.47.31.png
TVServiceExtensionを選択して追加

ServiceProvider.swift

ターゲットにTVServiceExtensionを追加するとServiceProvider.swiftがプロジェクトに追加される。
ServiceProvider.swiftはTVTopShelfProviderによっていくつかの機能が与えられたNSObjectのサブクラス。

topShelfStyle:TVTopShelfContentStyle

ここではシェルフのスタイルを返す
InsetとSectionedが選べる。

Sectioned

Simulator Screen Shot 2015.10.26 20.51.38.png

Inset

Simulator Screen Shot 2015.10.26 21.03.08.png

この違いは画像の大きさではなく、セクションのタイトルが表示されているか否かである。

topShelfItems: [TVContentItem]

ここでは表示するコンテンツを返す。


    var topShelfItems: [TVContentItem] {
        let contentIdentifier = TVContentIdentifier(identifier: "sample", container: nil)
        let contentItem = TVContentItem(contentIdentifier: contentIdentifier!)!
        let imageURL = NSBundle.mainBundle().URLForResource("Unknown", withExtension: "jpg")
        contentItem.imageURL = imageURL
        return [contentItem]
    }

Simulator Screen Shot 2015.10.26 21.44.27.png

imageURLにはhttps~から始まるURLも指定できる


    var topShelfItems: [TVContentItem] {
        let contentIdentifier = TVContentIdentifier(identifier: "sample", container: nil)
        let contentItem = TVContentItem(contentIdentifier: contentIdentifier!)!
        let imageURL = NSURL(string: "https://placeimg.com/640/480/any")
        contentItem.imageURL = imageURL
        return [contentItem]
    }

Simulator Screen Shot 2015.10.26 21.48.24.png

imageShape

imageShapeを指定すると、ポスターや四角などの大きさに画像をコンテンツを変更できる。
なおInsetの場合はExtraWideのみ、Sectionedの場合はPoster,Square,HDTVが選べる。

topShelfItems

TVContentItemは一枚のコンテンツとして以外にもコンテンツを束ねる役割もある。

    var topShelfItems: [TVContentItem] {
        let contentIdentifier = TVContentIdentifier(identifier: "sample", container: nil)
        let contentItem = TVContentItem(contentIdentifier: contentIdentifier!)!
        let imageURL = NSURL(string: "https://placeimg.com/640/480/any")
        contentItem.imageURL = imageURL
        contentItem.imageShape = .Poster
        let group = TVContentItem(contentIdentifier: contentIdentifier!)!
        group.topShelfItems = [contentItem,contentItem,contentItem,contentItem]
        group.title = "画像群"
        return [group]
    }

Simulator Screen Shot 2015.10.26 22.00.15.png

5
6
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
5
6