UITextView改变从纵向到横向的高度

时间:2012-11-30 10:27:39

标签: ios rotation uitextview

我有一个textView作为UIView的子视图,它是根据这个方案的scrollView的子视图:

-UIScrollView *scrollView
   -UIView *containerView
       -UITextView *textView
       -MKMapView *mapView
       -UIImageView *imageView

我在旋转过程中遇到问题,在从纵向到横向的过渡中,TextView被缩短,并且不再显示旋转前可见的文本的一部分。相反,从景观到肖像工作。我可以做什么? textView完全是用代码创建的,没有IBOutlet。

UITextView是可变高度,viewDidLoad中的contentSize设置如下:

 - (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}
      //........


    }

willRotateToInterfaceOrientation:duration看起来像:

-(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];
    }
}

处理帧旋转的两种方法是:

 -(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);
      //......
 }

我希望textView的高度在旋转期间保持固定。使用UIImageView和MKMapView工作。

1 个答案:

答案 0 :(得分:2)

试试这个:

  • 在viewDidLoad中,在init:

    之后添加此行

    self.textView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;

  • 在处理帧旋转方法时,将contentSize更改为frame:

    -(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.frame.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.frame.height);
        //......
    }
    

应该这样做(我在Windows中,所以我不能尝试)