how to set anchorPoint of view to 0.5 , 0.5 to get view in centre of screen? (Not scene)












0















I want to make my game full screen not with the black bars on both sides and bottom. so I found this code for that and it works for scene and now it's on full screen but the assets are not correctly positioned. Because of the view's coordinates are different (don't know why maybe because of previously I tried to set all for safeAreaLayoutGuide but then I get black bars). So is there any way to set it back to 0.5 0.5 . Otherwise I will have to set back anchorPoint of scene to what ever is view's anchorPoint and set all assets in my sks file according to that.



Here is the code from GameViewController :



class GameViewController: UIViewController {

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

if let view = self.view as! SKView? {
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true

let scene = HomeScene(size: view.bounds.size)
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}

override func viewDidLoad() {
super.viewDidLoad()

print("---")
print("∙ (type(of: self))")
print("---")
}
}


this code from HomeScene :



let rectangle1 = SKShapeNode(rect: (self.view?.bounds)!)
rectangle1.strokeColor = .white
rectangle1.lineWidth = 20
addChild(rectangle1)

// Set (0, 0) as the centre of the screen
scene?.anchorPoint = CGPoint(x: 0.5, y: 0.5)

let rectangle = SKShapeNode(rect: self.frame)
rectangle.strokeColor = .green
rectangle.lineWidth = 20
addChild(rectangle)


This is how it looks the scene is at 0.5, 0.5 but, view is above that green is scene and white is view and button in middle of the view :



enter image description here



Thank you.










share|improve this question

























  • Natarajan your answer is gone because of edit can you please answer it again.

    – ramesh sanghar
    Jan 2 at 10:42











  • Thanks Alexandru and .P van der laan for your answers. I did something else but it will help many others.

    – ramesh sanghar
    Mar 12 at 4:34


















0















I want to make my game full screen not with the black bars on both sides and bottom. so I found this code for that and it works for scene and now it's on full screen but the assets are not correctly positioned. Because of the view's coordinates are different (don't know why maybe because of previously I tried to set all for safeAreaLayoutGuide but then I get black bars). So is there any way to set it back to 0.5 0.5 . Otherwise I will have to set back anchorPoint of scene to what ever is view's anchorPoint and set all assets in my sks file according to that.



Here is the code from GameViewController :



class GameViewController: UIViewController {

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

if let view = self.view as! SKView? {
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true

let scene = HomeScene(size: view.bounds.size)
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}

override func viewDidLoad() {
super.viewDidLoad()

print("---")
print("∙ (type(of: self))")
print("---")
}
}


this code from HomeScene :



let rectangle1 = SKShapeNode(rect: (self.view?.bounds)!)
rectangle1.strokeColor = .white
rectangle1.lineWidth = 20
addChild(rectangle1)

// Set (0, 0) as the centre of the screen
scene?.anchorPoint = CGPoint(x: 0.5, y: 0.5)

let rectangle = SKShapeNode(rect: self.frame)
rectangle.strokeColor = .green
rectangle.lineWidth = 20
addChild(rectangle)


This is how it looks the scene is at 0.5, 0.5 but, view is above that green is scene and white is view and button in middle of the view :



enter image description here



Thank you.










share|improve this question

























  • Natarajan your answer is gone because of edit can you please answer it again.

    – ramesh sanghar
    Jan 2 at 10:42











  • Thanks Alexandru and .P van der laan for your answers. I did something else but it will help many others.

    – ramesh sanghar
    Mar 12 at 4:34
















0












0








0








I want to make my game full screen not with the black bars on both sides and bottom. so I found this code for that and it works for scene and now it's on full screen but the assets are not correctly positioned. Because of the view's coordinates are different (don't know why maybe because of previously I tried to set all for safeAreaLayoutGuide but then I get black bars). So is there any way to set it back to 0.5 0.5 . Otherwise I will have to set back anchorPoint of scene to what ever is view's anchorPoint and set all assets in my sks file according to that.



Here is the code from GameViewController :



class GameViewController: UIViewController {

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

if let view = self.view as! SKView? {
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true

let scene = HomeScene(size: view.bounds.size)
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}

override func viewDidLoad() {
super.viewDidLoad()

print("---")
print("∙ (type(of: self))")
print("---")
}
}


this code from HomeScene :



let rectangle1 = SKShapeNode(rect: (self.view?.bounds)!)
rectangle1.strokeColor = .white
rectangle1.lineWidth = 20
addChild(rectangle1)

// Set (0, 0) as the centre of the screen
scene?.anchorPoint = CGPoint(x: 0.5, y: 0.5)

let rectangle = SKShapeNode(rect: self.frame)
rectangle.strokeColor = .green
rectangle.lineWidth = 20
addChild(rectangle)


This is how it looks the scene is at 0.5, 0.5 but, view is above that green is scene and white is view and button in middle of the view :



enter image description here



Thank you.










share|improve this question
















I want to make my game full screen not with the black bars on both sides and bottom. so I found this code for that and it works for scene and now it's on full screen but the assets are not correctly positioned. Because of the view's coordinates are different (don't know why maybe because of previously I tried to set all for safeAreaLayoutGuide but then I get black bars). So is there any way to set it back to 0.5 0.5 . Otherwise I will have to set back anchorPoint of scene to what ever is view's anchorPoint and set all assets in my sks file according to that.



Here is the code from GameViewController :



class GameViewController: UIViewController {

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

if let view = self.view as! SKView? {
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true

let scene = HomeScene(size: view.bounds.size)
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}

override func viewDidLoad() {
super.viewDidLoad()

print("---")
print("∙ (type(of: self))")
print("---")
}
}


this code from HomeScene :



let rectangle1 = SKShapeNode(rect: (self.view?.bounds)!)
rectangle1.strokeColor = .white
rectangle1.lineWidth = 20
addChild(rectangle1)

// Set (0, 0) as the centre of the screen
scene?.anchorPoint = CGPoint(x: 0.5, y: 0.5)

let rectangle = SKShapeNode(rect: self.frame)
rectangle.strokeColor = .green
rectangle.lineWidth = 20
addChild(rectangle)


This is how it looks the scene is at 0.5, 0.5 but, view is above that green is scene and white is view and button in middle of the view :



enter image description here



Thank you.







ios swift sprite-kit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:47









Dávid Pásztor

22.6k83051




22.6k83051










asked Jan 2 at 10:08









ramesh sangharramesh sanghar

108




108













  • Natarajan your answer is gone because of edit can you please answer it again.

    – ramesh sanghar
    Jan 2 at 10:42











  • Thanks Alexandru and .P van der laan for your answers. I did something else but it will help many others.

    – ramesh sanghar
    Mar 12 at 4:34





















  • Natarajan your answer is gone because of edit can you please answer it again.

    – ramesh sanghar
    Jan 2 at 10:42











  • Thanks Alexandru and .P van der laan for your answers. I did something else but it will help many others.

    – ramesh sanghar
    Mar 12 at 4:34



















Natarajan your answer is gone because of edit can you please answer it again.

– ramesh sanghar
Jan 2 at 10:42





Natarajan your answer is gone because of edit can you please answer it again.

– ramesh sanghar
Jan 2 at 10:42













Thanks Alexandru and .P van der laan for your answers. I did something else but it will help many others.

– ramesh sanghar
Mar 12 at 4:34







Thanks Alexandru and .P van der laan for your answers. I did something else but it will help many others.

– ramesh sanghar
Mar 12 at 4:34














2 Answers
2






active

oldest

votes


















0














You could fix the problem with a different anchorpoint, or you could position the white 'view' node differently in the scene.



Information about the coordinate system (and anchor points): apple documentation



The default anchor position is (0.5, 0.5) according to the documentation. To put the white view in the bottom left: subtract half of the width and height of the x and y. Note that the line width also affects the positioning. Position the white view to the bottom left:



let bottomLeft: CGPoint = CGPoint(x: -self.size.width/2 + rectangle1.lineWidth/2,
y: -self.size.height/2 + rectangle1.lineWidth/2)
rectangle1.position = bottomLeft


To center the white view in the green view, subtract the width and height of rectangle1 (again, note the line width)



let center: CGPoint = CGPoint(x: -rectangle1.frame.size.width/2 + rectangle1.lineWidth/2,
y: -rectangle1.frame.size.height/2 + rectangle1.lineWidth/2)
rectangle1.position = center


Note that your green view is above the white view. Set the z position of the white view to some positive number to arrange it above the green view



rectangle1.zPosition = 1





share|improve this answer































    0














    If you were loading your HomeScene from a .sks file, the anchorPoint was set by xcode to (0.5, 0.5) as shown in my image.



    enter image description here



    But when you are trying to create a custom class without loading it from file (.sks), you will not have the properties set like here. So you need to set the manually.



    let scene = HomeScene(size: view.bounds.size) 
    // at this point, you HomeScene has the anchorPoint set to (0.0, 0.0)

    // lets set it back to (0.5, 0.5) as we want
    scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    scene.scaleMode = .aspectFill
    view.presentScene(scene)





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004414%2fhow-to-set-anchorpoint-of-view-to-0-5-0-5-to-get-view-in-centre-of-screen-no%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You could fix the problem with a different anchorpoint, or you could position the white 'view' node differently in the scene.



      Information about the coordinate system (and anchor points): apple documentation



      The default anchor position is (0.5, 0.5) according to the documentation. To put the white view in the bottom left: subtract half of the width and height of the x and y. Note that the line width also affects the positioning. Position the white view to the bottom left:



      let bottomLeft: CGPoint = CGPoint(x: -self.size.width/2 + rectangle1.lineWidth/2,
      y: -self.size.height/2 + rectangle1.lineWidth/2)
      rectangle1.position = bottomLeft


      To center the white view in the green view, subtract the width and height of rectangle1 (again, note the line width)



      let center: CGPoint = CGPoint(x: -rectangle1.frame.size.width/2 + rectangle1.lineWidth/2,
      y: -rectangle1.frame.size.height/2 + rectangle1.lineWidth/2)
      rectangle1.position = center


      Note that your green view is above the white view. Set the z position of the white view to some positive number to arrange it above the green view



      rectangle1.zPosition = 1





      share|improve this answer




























        0














        You could fix the problem with a different anchorpoint, or you could position the white 'view' node differently in the scene.



        Information about the coordinate system (and anchor points): apple documentation



        The default anchor position is (0.5, 0.5) according to the documentation. To put the white view in the bottom left: subtract half of the width and height of the x and y. Note that the line width also affects the positioning. Position the white view to the bottom left:



        let bottomLeft: CGPoint = CGPoint(x: -self.size.width/2 + rectangle1.lineWidth/2,
        y: -self.size.height/2 + rectangle1.lineWidth/2)
        rectangle1.position = bottomLeft


        To center the white view in the green view, subtract the width and height of rectangle1 (again, note the line width)



        let center: CGPoint = CGPoint(x: -rectangle1.frame.size.width/2 + rectangle1.lineWidth/2,
        y: -rectangle1.frame.size.height/2 + rectangle1.lineWidth/2)
        rectangle1.position = center


        Note that your green view is above the white view. Set the z position of the white view to some positive number to arrange it above the green view



        rectangle1.zPosition = 1





        share|improve this answer


























          0












          0








          0







          You could fix the problem with a different anchorpoint, or you could position the white 'view' node differently in the scene.



          Information about the coordinate system (and anchor points): apple documentation



          The default anchor position is (0.5, 0.5) according to the documentation. To put the white view in the bottom left: subtract half of the width and height of the x and y. Note that the line width also affects the positioning. Position the white view to the bottom left:



          let bottomLeft: CGPoint = CGPoint(x: -self.size.width/2 + rectangle1.lineWidth/2,
          y: -self.size.height/2 + rectangle1.lineWidth/2)
          rectangle1.position = bottomLeft


          To center the white view in the green view, subtract the width and height of rectangle1 (again, note the line width)



          let center: CGPoint = CGPoint(x: -rectangle1.frame.size.width/2 + rectangle1.lineWidth/2,
          y: -rectangle1.frame.size.height/2 + rectangle1.lineWidth/2)
          rectangle1.position = center


          Note that your green view is above the white view. Set the z position of the white view to some positive number to arrange it above the green view



          rectangle1.zPosition = 1





          share|improve this answer













          You could fix the problem with a different anchorpoint, or you could position the white 'view' node differently in the scene.



          Information about the coordinate system (and anchor points): apple documentation



          The default anchor position is (0.5, 0.5) according to the documentation. To put the white view in the bottom left: subtract half of the width and height of the x and y. Note that the line width also affects the positioning. Position the white view to the bottom left:



          let bottomLeft: CGPoint = CGPoint(x: -self.size.width/2 + rectangle1.lineWidth/2,
          y: -self.size.height/2 + rectangle1.lineWidth/2)
          rectangle1.position = bottomLeft


          To center the white view in the green view, subtract the width and height of rectangle1 (again, note the line width)



          let center: CGPoint = CGPoint(x: -rectangle1.frame.size.width/2 + rectangle1.lineWidth/2,
          y: -rectangle1.frame.size.height/2 + rectangle1.lineWidth/2)
          rectangle1.position = center


          Note that your green view is above the white view. Set the z position of the white view to some positive number to arrange it above the green view



          rectangle1.zPosition = 1






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 5 at 18:56









          P. van der LaanP. van der Laan

          1017




          1017

























              0














              If you were loading your HomeScene from a .sks file, the anchorPoint was set by xcode to (0.5, 0.5) as shown in my image.



              enter image description here



              But when you are trying to create a custom class without loading it from file (.sks), you will not have the properties set like here. So you need to set the manually.



              let scene = HomeScene(size: view.bounds.size) 
              // at this point, you HomeScene has the anchorPoint set to (0.0, 0.0)

              // lets set it back to (0.5, 0.5) as we want
              scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
              scene.scaleMode = .aspectFill
              view.presentScene(scene)





              share|improve this answer




























                0














                If you were loading your HomeScene from a .sks file, the anchorPoint was set by xcode to (0.5, 0.5) as shown in my image.



                enter image description here



                But when you are trying to create a custom class without loading it from file (.sks), you will not have the properties set like here. So you need to set the manually.



                let scene = HomeScene(size: view.bounds.size) 
                // at this point, you HomeScene has the anchorPoint set to (0.0, 0.0)

                // lets set it back to (0.5, 0.5) as we want
                scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
                scene.scaleMode = .aspectFill
                view.presentScene(scene)





                share|improve this answer


























                  0












                  0








                  0







                  If you were loading your HomeScene from a .sks file, the anchorPoint was set by xcode to (0.5, 0.5) as shown in my image.



                  enter image description here



                  But when you are trying to create a custom class without loading it from file (.sks), you will not have the properties set like here. So you need to set the manually.



                  let scene = HomeScene(size: view.bounds.size) 
                  // at this point, you HomeScene has the anchorPoint set to (0.0, 0.0)

                  // lets set it back to (0.5, 0.5) as we want
                  scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
                  scene.scaleMode = .aspectFill
                  view.presentScene(scene)





                  share|improve this answer













                  If you were loading your HomeScene from a .sks file, the anchorPoint was set by xcode to (0.5, 0.5) as shown in my image.



                  enter image description here



                  But when you are trying to create a custom class without loading it from file (.sks), you will not have the properties set like here. So you need to set the manually.



                  let scene = HomeScene(size: view.bounds.size) 
                  // at this point, you HomeScene has the anchorPoint set to (0.0, 0.0)

                  // lets set it back to (0.5, 0.5) as we want
                  scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
                  scene.scaleMode = .aspectFill
                  view.presentScene(scene)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 16 at 7:54









                  Alexandru VasiliuAlexandru Vasiliu

                  15913




                  15913






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004414%2fhow-to-set-anchorpoint-of-view-to-0-5-0-5-to-get-view-in-centre-of-screen-no%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith