2
votes

I have a Swift + SpriteKit game in development, in which I would like to insert a small UITableViewController into a small (100 x 200) UIView, and then add that view to my SKScene. I am using this code:

class BugsScene: SKScene {

    override func didMoveToView(view: SKView) {

        anchorPoint = CGPoint(x: 0.5, y: 0.5)

        NSUserDefaults.standardUserDefaults().setObject("bugs", forKey: "lastSceneViewed")

        let bugsTableView: BugsTVC = BugsTVC()
        let nav = UINavigationController(rootViewController: bugsTableView)

        let smallerRect = CGRectMake(0, 0, 100, 200)
        let smallerView = UIView(frame: smallerRect)
        smallerView.addSubview(nav.view)
        self.view.addSubview(smallerView)
    }
}

However, in landscape mode (my app only supports landscape left and landscape right, NO portrait), the tableview occupies the entire height of the landscape mode, and has the width of an iPad in portrait.

How do I add a small UIView to a bigger scene in swift/spritekit?

1
change the view's frame property - LearnCocos2D
So in the code above, I set the frame of the UIView when I initialize it. Do you mean something different from this? Setting the frame of the smaller view separately from the init doesn't seem to have an affect. - zeeple
@LearnCocos2D Your comment was dead-on. My problem was that I was failing to set the frame of the nav controller, which the tableview sits in. Turn this into an answer and I'll select it as correct. Thanks! - zeeple

1 Answers

2
votes

LearnCocos2D definitely deserves credit for this, so if he ever posts an answer I will accept it. Essentially, I was failing to set the frame for the navigation controller's view. Here is my method for adding the smaller view to the scene:

override func didMoveToView(view: SKView) {

        anchorPoint = CGPoint(x: 0.5, y: 0.5)

        self.name = "Bugs Scene"
        backgroundColor = bugsColor

        let smallerRect = CGRectMake(400, 24, 600, 720)
        let navRect = CGRectMake(0, 0, 600, 720)
        let bugsTableView: BugsTVC = BugsTVC()
        nav = UINavigationController(rootViewController: bugsTableView)
        nav.view.frame = navRect
        smallerView = UIView(frame: smallerRect)
        smallerView.backgroundColor = UIColor.clearColor()

        bugsTableView.view.frame = smallerRect
        smallerView.frame = smallerRect
        smallerView.addSubview(nav.view)
        self.view.addSubview(smallerView)


    }

It could probably be refined some but this is working for me. Now if I can only figure out how to remove the view when I transition to another scene!