1

I have subClass an UIView and override the layoutSubviews method. When I called this subClass in my viewController,and add it to the controller's view,I found that "layoutSubviews" function have been called twice.

  coverFlowView = CoverFlowView(frame: CGRectMake(0, 40, UIScreen.mainScreen().bounds.size.width, 480))
coverFlowView.delegate = self
coverFlowView.dataSource = self
self.view.addSubview(coverFlowView)

CoverFlowView is a view subclass UIView

Eric
  • 99
  • 1
  • 7
  • That's the way it works. I have a controller where this is called kind of five times. Depends on the controller and how it is setup. Write code that can deal with this situation. – dasdom Nov 06 '15 at 10:14
  • without any other code, such as change frame or rotate,just addSubView.How can i deal with this situation? – Eric Nov 06 '15 at 10:16
  • Write the code in `layoutSubviews` in a way that it can be called several times. In general try to avoid overriding `layoutSubviews`. – dasdom Nov 06 '15 at 10:19

1 Answers1

0

I encountered the same problem.I tried different OS and got different result for same code flag:

one device only call layoutSubViews once, but another one called twice which caused one bug for my code...

My Code:

CGFloat step = 0.0;

    if (self.array_rateOfEachColor) {

        CGFloat sumRate = 0.0;
        for (NSString *rate in self.array_rateOfEachColor) {

            sumRate +=[rate floatValue];

        }

        NSAssert(sumRate != 1.0 , @" sum of all elemetns for array array_rateOfEachColor must be 1.0 ");

        for (int i = 0 ; i < [self.mutableArray_layers count]; i ++) {

            CGFloat step = [[_array_rateOfEachColor objectAtIndex:i] floatValue];

            CAShapeLayer *tmp = [self.mutableArray_layers objectAtIndex:i];

            [tmp setStrokeStart:self.strokeEndValue];

            if (i == (self.mutableArray_layers.count -1)) { //the last layer

                self.strokeEndValue = 1.0 - self.space ;
                [tmp setStrokeEnd:self.strokeEndValue];
            }else {

                [tmp setStrokeEnd:self.strokeEndValue + step ];
            }

            self.strokeEndValue += (step + self.space); // record last strokeEndValue
        }

    }else{

        step = 1.0 / self.mutableArray_layers.count; //average step

        for (int i = 0 ; i < [self.mutableArray_layers count]; i ++) {

            CAShapeLayer *tmp = [self.mutableArray_layers objectAtIndex:i];

            [tmp setStrokeStart:self.strokeEndValue];

            if (i == (self.mutableArray_layers.count -1)) { //the last layer

                self.strokeEndValue = 1.0 - self.space ;
                [tmp setStrokeEnd:self.strokeEndValue];
            }else {

                [tmp setStrokeEnd:self.strokeEndValue + step ];
            }

            self.strokeEndValue += (step + self.space); // record last strokeEndValue

        }
    }
    self.strokeEndValue  = 0.0;  // different os , the layoutSubViews was called different times,so,this line is resolve this problem so that figure can be shown correctly
}
aaisataev
  • 1,643
  • 3
  • 22
  • 38
Billy Chen
  • 21
  • 3