Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retina issue: how to make UIView to have width exactly 1 of pixel instead of 2 pixels?

I know we are operating on points not pixels and in most cases it's convenient, but I need to make UIView be 1 pixel instead of 2 pixel height. So, if you drag and drop some UIView (separator line) in Interaface builder, and make it the height of 1px (point) then it will still look like 2 pixel size line on retina screen (both on device and simulator).

I know there contentScaleFactor property on the view which show is it retina (2.0f) or not (1.0f). It looks like the views has the value of 1.0f, so you need to retrieve that from main screen:

[UIScreen mainScreen].scale; 

This returns me 2.0f. Now, I'v added height constraint for this separator view added the method which checks isRetina and divides the line to make it exactly 1 pixel:

- (void)awakeFromNib{

  [super awakeFromNib];

  CGFloat isRetina = ([UIScreen mainScreen].scale == 2.0f) ? YES : NO;

  if (isRetina) {

    self.separatorViewHeightConstraint.constant /= 2;
  }
}

This works, I'm just not sure is it good idea to use 0.5 value ...

like image 709
Centurion Avatar asked Jan 27 '14 15:01

Centurion


1 Answers

To support newer 3x displays (e.g. iPhone 6+) use this code:

UIScreen* mainScreen = [UIScreen mainScreen];
CGFloat onePixel = 1.0 / mainScreen.scale;
if ([mainScreen respondsToSelector:@selector(nativeScale)])
    onePixel = 1.0 / mainScreen.nativeScale;
like image 188
Alejandro Avatar answered Nov 15 '22 21:11

Alejandro