Using TVPosterImage in TVUIKit for tvOS 12

2019-03-04 11:25发布

问题:

tvOS 12 has a new framework TVUIKit, which introduces the lockup views. The class I am interested in is TVPosterView, which is basically designed as such:

Swift 4.2

open class TVPosterView : TVLockupView { // One may use UIControl to implement it in iOS
    public init(image: UIImage?)
    open var image: UIImage? // default is nil
    open var title: String?
    open var subtitle: String?
}

In my storyboard I have added an UIView element, and rightly (at least I hope so. Yesterday the IB agent kept crashing!) changed its class to TVPosterView in Identity Inspector. then in my UIViewController I have defined:

@IBOutlet var aPosterView: TVPosterView!

override func viewDidLoad() {
        super.viewDidLoad()

        if let myIcon = UIImage(named: "icon.png") {
            self.aPosterView = TVPosterView(image: myIcon)
            self.aPosterView.title = "My Title"
            self.aPosterView.subtitle = "A Sub Title"
        }
        else {
            print("ERROR: I couldn't load the icon!")
        }
    }
}

It compiles without any warning. When I run it, it just shows the UIView white background, nothing changes. I did some tests trying to add an UIImageView, but they were all inconclusive (apart, off course, when I did set the image of UIImageView to self.aPosterView.image).

I am far from being an expert, I just started learning a couple of weeks ago. Any idea about what I am missing? I thought the concept was that once I initiated the class the framework was taking care of displaying the poster with the (optional) title and subtitles, also taking care of all nice animations!

回答1:

Eventually I managed to make it working programmatically. I tested both TVPosterView and TVMonogramView (basically a round button). The following code works with Xcode 10 beta 5, on tvOS 12 beta 5:

Swift 4.2

import UIKit
import TVUIKit

class SecondViewController: UIViewController {

    var myPoster = TVPosterView()
    var myMonogram = TVMonogramView()

    override func viewDidLoad() {
        super.viewDidLoad()

        myPoster.frame = CGRect(x: 100, y: 100, width: 550, height: 625)
        myPoster.image = UIImage(named: "image1.png")
        myPoster.imageView.masksFocusEffectToContents = true
        myPoster.title = "Poster"
        myPoster.subtitle = "This is the poster subtitle"

        self.view.addSubview(myPoster)

        var myName = PersonNameComponents()
        myName.givenName = "Michele"
        myName.familyName = "Dall'Agata"

        myMonogram.frame = CGRect(x: 700, y: 100, width: 500, height: 475)
        myMonogram.image = UIImage(named: "image2.png")
        myMonogram.title = "Monogram"
        myMonogram.subtitle = "This is the Monogram subtitle"
        myMonogram.personNameComponents = myName

        self.view.addSubview(myMonogram)

        print(myMonogram.personNameComponents)
    }
}

I didn't manage to scale the poster's image with scaleAspectFit, though. Also my first image was rounded with a transparency, and only choosing a frame size that perfectly fit the squared image plus the titles (so no aspect fit needed), the glowing effect became transparent on the corners. Otherwise the whole image was opaque, using its own (quite small) rounded corners.