How do I create a 1px line in Interface Builder?

后端 未结 10 1781
独厮守ぢ
独厮守ぢ 2020-12-04 10:30

Note, I\'m looking to make a 1px line, not a 1pt line. Meaning it should be 1px regardless of screen scale (so 0.5pt on Retina devices).

I can do this programmatica

10条回答
  •  半阙折子戏
    2020-12-04 11:13

    By creating this tiny subclass of NSLayoutConstraint I'm now able to add 1px lines in IB:

    @implementation NSLayoutConstraintHairline
    
    -(void)awakeFromNib
    {
        [super awakeFromNib];
        if ( self.constant == 1 ) self.constant = 1/[UIScreen mainScreen].scale;
    }
    
    @end
    

    Any constraint with a value of 1 can be set to class NSLayoutConstraintHairline to make the constant effectively 1px instead of 1pt.

    If you ever decide to change the constant to another value, it will just work as any other constraint.

提交回复
热议问题