How to set the initial focus on the buttons in the tvOS view controller (AppleTV)?

I added a few buttons to the Swift game for tvOS.

override func didMoveToView(view: SKView) {

    let button1=UIButton(frame: CGRectMake(330, 800, 300, 100))
    button1(imageBuySelected, forState: UIControlState.Focused)
    button1(imageBuyNormal, forState: UIControlState.Normal
    button1 = UIColor.clearColor()
    button1(self, action: "buy1", forControlEvents: UIControlEvents.PrimaryActionTriggered)
    self.view!.addSubview(button1)

    let button2=UIButton(frame: CGRectMake(330, 800, 300, 100))
    button2(imageBuySelected, forState: UIControlState.Focused)
    button2(imageBuyNormal, forState: UIControlState.Normal
    button2 = UIColor.clearColor()
    button2(self, action: "buy2", forControlEvents: UIControlEvents.PrimaryActionTriggered)
    self.view!.addSubview(button2)

    let button3=UIButton(frame: CGRectMake(330, 800, 300, 100))
    button3(imageBuySelected, forState: UIControlState.Focused)
    button3(imageBuyNormal, forState: UIControlState.Normal
    button3 = UIColor.clearColor()
    button3(self, action: "playGame", forControlEvents: UIControlEvents.PrimaryActionTriggered)
    self.view!.addSubview(button3)
}

When will the games begin. The initial focus is set to button1. I want the focus to be set to button3. I understand that the preferred focus is automatically selected based on the first object of the focused object, which starts from the upper left corner of the screen. Does anyone know how to override this so that I can make button3 the preferred focus at the start of the game?

+4
source share
2 answers

preferredFocusedView button3.

Preferred Focus Chain .

+6

, , , !:)

override var preferredFocusedView: UIView? {
    get {
        return self.button3
    }
}
+5

Source: https://habr.com/ru/post/1608141/


All Articles