0

My code is as bellow:

struct Clock10View: View {
    let light: Bool
    var body: some View {
        ZStack() {
            Rectangle()
                .frame(width: 6, height: 6)
                .foregroundColor(.gray)
            Rectangle()
                .frame(width: 5, height: 5)
                .foregroundColor(.red)
        }
    }
}

I copied a screenshot of the simulator to Figma, enlarged it, and found that the center points of the two Rectangle were not completely aligned.

enter image description here

How do I need to align the two blocks completely?

mars
  • 109
  • 5
  • 21
  • You have a 5x5 and a 6x6 and you want them to be centered perfectly in a 7x7? – ColdLogic Jul 09 '23 at 03:49
  • @ColdLogic It appears that 6 * 6 is completely centered, but 5 * 5 is offset by 0.5 pixels. It appears that Swiftui is aligned as an integer and will be rounded when centered? – mars Jul 09 '23 at 04:03
  • I havent looked into the specifics of the technology, but obviously if you wanted a pixel perfect centering, it would be impossible since you cant make 1/2 a pixel a certain color. Although a specific device may have 2x the pixel density such that it "could" do a pseudo 1/2 a pixel, it wouldn't be device independent. I imagine your problem is from the sizing and not from SwiftUI itself in that if you increased the sizes to a evenly divisible value, it would appear as you expect. I don't think I have a good answer as to how to help solve your problem if these are the sizes you need. – ColdLogic Jul 09 '23 at 04:16
  • SwiftUI rounds to the nearest pixel, so not sure if you can avoid this – aheze Jul 09 '23 at 04:56
  • @ColdLogic Thanks for your help. I have try to make 1/2 a pixel offset, but it will shifting more from center – mars Jul 09 '23 at 13:32

0 Answers0