I have a textView as a subview of a UIView, which is a subview of a scrollView according to this scheme:
-UIScrollView *scrollView
-UIView *containerView
-UITextView *textView
-MKMapView *mapView
-UIImageView *imageView
I have a problem during rotation, in the transition from portrait to landscape the TextView is shortened, and is no longer displayed part of the text that was visible before the rotation. In step contrary, from landscape to portrait work.Cosa can I do? The textView is created entirely in code, there isn’t an IBOutlet.
The UITextView is variable height, the contentSize in viewDidLoad is set as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
//....
self.textView = [[UITextView alloc]init];
self.textView.backgroundColor =[UIColor clearColor];
CGRect frame;
frame = self.textView.frame;
frame.size.height= [self.textView contentSize].height;
[self.textView setContentSize:self.textView.contentSize];
self.textView.frame = frame;
NSLog(@"The contentSize of TextView is %@",NSStringFromCGSize
(self.textView.contentSize));
//The contentSize of TextView is {0, 161032}
//........
}
The willRotateToInterfaceOrientation:duration looks like:
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
CGSize containerSize = CGSizeMake(768, 5000);
self.scrollView.contentSize = containerSize;
self.containerView.frame = CGRectMake(0, 0, 768, 5000);
[self layoutPortrait];
}
else if (deviceOrientation== UIInterfaceOrientationLandscapeRight){
CGSize containerSize = CGSizeMake(1004, 5000);
self.scrollView.contentSize = containerSize;
self.containerView.frame = CGRectMake(0, 0, 1024, 5000);
[self layoutLandscape];
}
else if (deviceOrientation==UIInterfaceOrientationLandscapeLeft){
CGSize containerSize = CGSizeMake(1004, 5000);
self.scrollView.contentSize = containerSize;
self.containerView.frame = CGRectMake(0, 0, 1024, 5000);
[self layoutLandscape];
}
}
And the two methods that handle frames rotation are:
-(void) layoutPortrait{
//...
NSLog(@"the height of textView è %f", self.textView.contentSize.height);
//the height of textView è 161032.000000
self.textView.frame =CGRectMake(56, 490, self.viewImages.frame.size.width, self.textView.contentSize.height);
//.....
}
-(void) layoutLandscape{
//......
NSLog(@"The height of textView è %f", self.textView.contentSize.height);
/*The height of textView è 2872.000000
(This after rotation from portrait to landscape)*/
self.textView.frame = CGRectMake(170, 495, self.viewImages.frame.size.width, self.textView.contentSize.height);
//......
}
I want that the height of textView remain fixed during rotation. With UIImageView and MKMapView work.
Try this:
In viewDidLoad add this line after the init:
self.textView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;At Handle frames rotation methods, change contentSize to frame:
That should do the trick (I’m in windows so i can’t try it)