UIView's content blurry when I center it

2019-07-12 04:36发布

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. enter image description here

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 :)

enter image description here

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

标签: ios uiview
2条回答
Juvenile、少年°
2楼-- · 2019-07-12 05:06

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.

查看更多
老娘就宠你
3楼-- · 2019-07-12 05:17

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

查看更多
登录 后发表回答