coding22 coding22 - 3 months ago 21
iOS Question

How do I make a border around my whole screen for all the devices in Swift Xcode?

I have this game thats in landscape and I need a border around the whole screen so my heroNode can't go out of the screen on either of the four sides. I have this code but it only works for the iphone 5s and its too small for the other devices. How would I get the code to resize and fit the screen for the other devices? Thanks!

override func didMoveToView(view: SKView) {
let borderBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: 20, y: -90, width: self.size.width-50, height: self.size.height))
borderBody.categoryBitMask = borderbodycategory
borderBody.collisionBitMask = HeroCategory
borderBody.contactTestBitMask = HeroCategory
borderBody.allowsRotation = false
borderBody.affectedByGravity = false
self.physicsBody = borderBody

}

Answer

You can use UIScreen 's property instead of the fixed value.

let width = UIScreen.mainScreen().bounds.size.width - 50
let height = UIScreen.mainScreen().bounds.size.height - 50
let borderBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: 20, y: -90, width: width, height: height))