gpt4 book ai didi

ios - UITextView 改变高度从纵向到横向

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:38 25 4
gpt4 key购买 nike

我有一个 textView 作为 UIView 的 subview ,根据此方案,它是 scrollView 的 subview :

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

最佳答案

试试这个:

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

关于ios - UITextView 改变高度从纵向到横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13643336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com