Unwanted space between subviews while subviews height value as decimal value

Description:


We are adding UILabels as subviews of UIView on vertical manner and height of the subviews as calculated by current display width. If height of the subview as decimal value then unwanted spaces will be displayed between subviews.


Steps to Reproduce:


  1. Run the attached project
  2. See the reported issue


Expected Behavior:


Remove the unwanted space between the subviews


Actual Behavior:


There is some unwanted space between the subviews


Code Snippet :

- (void)viewDidLoad {

[super viewDidLoad];

[self view].backgroundColor = UIColor.redColor;


//// If the height value has decimal value then the issue will raise.

double heightValue = 36.60f;

double offsetValue = 100;


for (int i = 0; i < 10; i++)

{

UILabel* label = [[UILabel alloc] init];

label.backgroundColor = UIColor.whiteColor;

label.text = @"Demo";

label.frame = CGRectMake (offsetValue, offsetValue + i * heightValue, offsetValue, heightValue);

[self.view addSubview:label];

}

}


Sample: https://drive.google.com/open?id=1Lf1JXbEWupwMoh2A5ohcolSLgJDrGjCp

ScreenShots: https://drive.google.com/open?id=1VUY6-Sv34_vScqs1Qoh_lCRSMt137ER4

Replies

You wrote:

//// If the height value has decimal value then the issue will raise.
double heightValue = 36.60f;

What do you mean by “decimal value” here? Can you give us an example of a value that causes problems and a value that works?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Eskimo,


Thanks for your response,


If we set the height of the view as decimal value(36.60f), unwanted spaces or lines are shown between subviews. But we set the height as 36, 37,.. any int value then the spaces or lines are not shown between subviews.


Sample: https://drive.google.com/open?id=1Lf1JXbEWupwMoh2A5ohcolSLgJDrGjCp

ScreenShots: https://drive.google.com/open?id=1VUY6-Sv34_vScqs1Qoh_lCRSMt137ER4

Ah, I see where you’re going here. The source of my confusion is your use of the term decimal. There is a similarly named type (

Decimal
in Swift,
NSDecimal
in Objective-C) but it’s generally not used for view layout. The UIKit API always works in terms of
CGFloat
, which is a binary floating point type (either
Float
or
Double
depending on your environment).

In UIKit, a

CGFloat
with no fractional part is aligned to a point boundary, which is the logical unit of layout. Below that there are pixel boundaries, which correspond to pixels on the screen. Historically these were the same, but Retina displays, and later Super Retina displays, introduce a mapping between the two. For example, a Retina display has a 2x mapping, meaning that there are 2 pixel boundaries for every point boundary.

Doing layout off point boundaries is tricky, and doing layout off pixel boundaries is even trickier. I don’t have enough experience with UIKit to offer concrete advice on that topic. I’ve moved your thread over to App Frameworks > Cocoa Touch and hopefully someone there can help out.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Any Update on this?