2

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

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

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

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

 - (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 一起工作。

4

1 回答 1

2

试试这个:

  • 在 viewDidLoad 在 init 之后添加这一行:

    self.textView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;

  • 在处理框架旋转方法中,将 contentSize 更改为框架:

    -(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中,所以我无法尝试)

于 2012-12-19T18:06:41.677 回答