Best way to resize UIView when rotating to terrain and back
Since I am very new to ios programming I have more of a general design question. I have a ViewController that contains a GraphView (UIScrollView + UIView) that works great. When I rotate to a landscape, I want the GraphView to resize its height to the height of the display (so that it fills the entire screen), but only 300px when in a portrait.
What I have done so far is to implement viewWillLayoutSubviews
in the ViewController and reset the constraints:
- (void)viewWillLayoutSubviews{
_graphViewHeightConstraint.constant = ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) ? 300:[[UIScreen mainScreen] bounds].size.height-self.navigationController.navigationBar.frame.size.height - 2*_distanceToTopView.constant;
}
and in GraphView.m:
- (void)layoutSubviews{
kGraphHeight = self.frame.size.height;
[self setNeedsDisplay];
}
(because I need the kGraphHeight variable in the code to draw the graph). This doesn't seem like a very elegant solution, so I wanted to ask what would be the best way? Thanks a lot for your inputs :)
source to share
In GraphView.m
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
kViewWidth = <GET_SCREEN_WIDTH_HERE>;
kViewHeight = <GET_SCREEN_HEIGHT_HERE>;
[self updateViewDimensions];
}
and updateViewDimensions will set UIScrollView and UIView frame
- (void)updateViewDimensions
{
scrollView.frame = self.view.frame;
yourView.frame = CGRectMake(kViewXStartsFrom, kViewYStartsFrom, kViewWidth, kViewHeight);
}
after turning to Landscape viewDidLayoutSubviews will be called.
This works for me.
source to share