iosuiviewcore-graphicsquartz-core

redraw a custom uiview when changing a device orientation


  1. If I draw my chart inside - (void)drawRect:(CGRect)rect is just enough to set [_chartView setContentMode:UIViewContentModeRedraw] and this method will be called when device changes it's orienatation and it's possible to calculate f.e. new center point for my chart.
  2. If I create a view like a subview using - (id)initWithFrame:(CGRect)frame and then add it in view controller like [self.view addSubview:chartView];. How in this case I can handle rotation to redraw my chart?

Solution

  • To make your chart rendered correctly when device orientation changes you need to update chart's layout, here is the code that you should add to your view controller:

    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
    
        _chartView.frame = self.view.bounds;
        [_chartView strokeChart];
    }