0

Update: It looks like that it only happens on notch-less models. You don't need to do anything on newer models to make both swiftui and the bounds coordinate anchored on the same center.

The red star is the default center on the View. The black star is the "UIScreen midX" which is offset by the status bar I guess. Do I need to calculate the status bar height to center the black star? Thank you.

ZStack{

         Text("✧")
            .position(x: UIScreen.main.bounds.minX, y: UIScreen.main.bounds.minY)
         Text("✧")
            .foregroundColor(Color.red)
         Text("✧")
            .position(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY)
 }

enter image description here

William Tong
  • 479
  • 7
  • 14
  • 1
    Depending on your long-term goal, I'd recommend avoiding using `UIScreen` in SwiftUI whenever possible and instead using the built-in tools for screen-size independent layout, or, if need be, relying on `GeometryReader`. – jnpdx Oct 22 '21 at 04:56
  • 1
    `UIScreen` gives size of full screen, but your container is constructed within safe area which is smaller and might be shifted due to upper notch, so it is not correct instrument for layout, use instead recommendation from previous comment. – Asperi Oct 22 '21 at 05:05
  • Thank you for your comments. They are some visual effects and I want more control. So I decided to reply on the coordinates system. – William Tong Oct 22 '21 at 07:01

1 Answers1

0

I came across the issue again. And I found that the difference between the swiftui default center point and the UIScreen.main.bounds.height/2 is different across difference device models. You need to use the GeometryReader to get the View's default center and then pass it to other subviews or UIViewRepresentalbe so that the center of other subviews could be aligned to the SwiftUI default view center.

GeometryReader{ proxy in
    
   ZStack{

       Text("✧")
          .position(x: proxy.size.width/2, y: proxy.size.height/2)
       Text("✧")
          .foregroundColor(Color.red)
      

     }
}
William Tong
  • 479
  • 7
  • 14