1 Reply
      Latest reply on Sep 11, 2019 10:13 PM by PBK
      Sasi@3595 Level 1 Level 1 (0 points)

        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

        • Re: Unwanted space between subviews while subviews height value as decimal value
          PBK Level 7 Level 7 (3,235 points)

          This could be because of a round-off error in CGRectMake.

          The second view is supposed to go from 136.6 to 173.2 but it could start at 136 (i.e. 100 + int(36.6) = 136 ) and go only to 172  (i.e. start at 136 and extend for 36; that is 136+36=172 ) because your fourthvariable in CGRectMake is 36.6 which could become 36.

          The next view will start at int(100 +73.2)=int(173.2)=173.

           

          Why not use integers and control the rounding?  Calculate a start value = int(100 + i*36.6) and an end value = int(100+(i+1)*36.6).  Then use (end-start) as the fourth variable in CGRectMake not 36.6. That will make your views not 36.6 and not 35 but rather sometimes 36 and sometimes 37.