- (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.- (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?While a preferred solution requires zero lines of code, if you must trigger a redraw, do so in setNeedsDisplay
, which in turn invokes drawRect
.
No need to listen to notifications nor refactor the code.
Swift
override func layoutSubviews() {
super.layoutSubviews()
self.setNeedsDisplay()
}
Objective-C
- (void)layoutSubviews {
[super layoutSubviews];
[self setNeedsDisplay];
}
Note:
layoutSubviews
is a UIView
method, not a UIViewController
method.