UIView's content blurry when I center it

2019-07-12 04:15发布

问题:

I have UIView that isn't big as the whole screen and I want it to appear in the middle of the screen. I did that with the following code:

    self.dialogView.center = CGPointMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0);

Unfortunately, when I center it like that, UIView's content appears blurry.

I've fixed it with this:

    self.dialogView.center = CGPointMake(self.view.bounds.size.width / 2.0 + 0.5, self.view.bounds.size.height / 2.0);

Now UIView's content appears as it should :)

Can you tell me why that content becomes blurry and why I need to add 0.5 to the left position offset?

回答1:

The new center is causing the origin of the control's view to fall on a half-pixel, like { 20.5, 20.5 }. Since the screen can't draw only half a pixel, the rendering engine attempts to get the same effect by diffusing the image as best it can, resulting in blurry images.

You need to make sure all your views' origins are whole numbers.



回答2:

well i think thats due to how the UIView is being placed on the pixel grid of the screen. if you think about it on regular screens (72 px/inch), 0.5 points means 0.5 pixels which would probably cause what you see... thats my theory at least :P



标签: ios uiview