self.layer.borderWidth = 0.5;
on a UIButton
or UITextField
render fine on a retina screen, but on a non-retina screen only the top and left borders render while the right and bottom borders do not render.
I assume it has something to do with dpi of the screen and how sub point lines are drawn, but it's possible that there is a better explanation.
Question:
I'd like to know if it's possible to have all sides of a UIView
's border show as expected on both retina and non-retina screens with borderWidth
set to 0.5
.
If you want a single pixel (not point) line always, you'll have to use a different border width depending on the scale of the screen.
E.g.:
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale == 2.0) {
// retina screen;
self.layer.borderWidth = 0.5;
} else {
// non-retina screen
self.layer.borderWidth = 1.0;
}
Now that multiple scales are supported (@3x) it is probably better to write Matt's answer as this:
CGFloat scale = [[UIScreen mainScreen] scale];
CGFloat width = scale > 0.0 ? 1.0 / scale : 1.0;
[self.layer setBorderWidth:width];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With