简体   繁体   English

iOS Textviews在横向滚动视图中未扩展

[英]iOS textviews not expanding in scrollview in landscape

I had a view with some textviews in it that were working as I wanted. 我有一个带有一些textview的视图,可以根据需要运行。 The expanded to a the same right-margin whether in landscape or portrait. 不论横向还是纵向,都将扩展到相同的右边界。

I recently have tried changing the normal view to a scrollview. 我最近尝试将普通视图更改为滚动视图。 I've had no luck getting these text views to expand as they once did, though. 但是,我无法让这些文本视图像以前一样扩展。 When in landscape mode everything stays huddled over on the left side with the same width as a portrait phone. 在横向模式下,所有内容都停留在左侧,宽度与纵向电话相同。

Here is some code. 这是一些代码。

- (void)viewDidLoad {
    CGSize screen = [self handleScreenOrientation];
    [(UIScrollView *)self.view setContentSize:CGSizeMake(screen.width, screen.height)];
}

- (CGSize)handleScreenOrientation {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown ) {
        return CGSizeMake(screenRect.size.width, screenRect.size.height);
    }
    else {
        return CGSizeMake(screenRect.size.height, screenRect.size.width);
    }
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    CGSize screen = [self handleScreenOrientation:toInterfaceOrientation];
    UIScrollView *scrollView = (UIScrollView *)self.view;
    scrollView.frame = CGRectMake(0, 0, screen.width, screen.height);
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
    [scrollView setNeedsDisplay];
}

The method handleScreenOrientation with the passed orientation is the same as the one w/ no parameters, just it uses the passed orientation instead of the current orientation of the status bar. 具有传递方向的方法handleScreenOrientation与不带参数的方法相同,只是它使用传递方向而不是状态栏的当前方向。

I've checked and my scrollview is set to autoresize subviews. 我已经检查过,并且我的scrollview设置为autoresize子视图。

Any ideas would be much appreciated. 任何想法将不胜感激。 Thanks. 谢谢。

Update : I have added 更新 :我已经添加

self.view.autoresizesSubviews = true;

for (UIView *view in self.view.subviews) {
    view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
}

to viewdidload and willrotatetointerfaceorientation. 查看加载和将旋转到界面方向。 No change. 没变。

I think the problem is that you are overriding the autoresizingMasks by forcing a change to the scrollView's contentSize. 我认为问题在于,您通过强制更改scrollView的contentSize来覆盖autoresizingMask。 Although you have correctly noted that the screenSize is always in portrait orientation, so you reverse its dimensions for landscape, you are determining which orientation to use before the device actually rotates. 尽管您已经正确地注意到screenSize始终为纵向,所以您将其尺寸反转为横向,但是您要确定设备实际旋转之前要使用的方向。 So you're forcing the scrollView to maintain portrait dimensions in landscape. 因此,您将强制scrollView保持横向的纵向尺寸。

Keep your autoresizeMask routine and discard your willRotateToInterfaceOrientation routine. 保留autoresizeMask例程,并丢弃您的willRotateToInterfaceOrientation例程。 Does it work now? 现在可以用吗?

If not, try putting the willRotate code into *did*RotateToInterfaceOrientation. 如果不是,请尝试将willRotate代码放入* did * RotateToInterfaceOrientation。


Another idea: 另一个想法:

I notice that in viewDidLoad you cast the container view ( self.view ) to a UIScrollView. 我注意到在viewDidLoad您将容器视图( self.viewself.view转换为UIScrollView。 I wonder if it might be better to leave the container view as a generic UIView, add a UIScrollView as a subview, and then add the textviews to the scrollview. 我想知道将容器视图保留为通用UIView,将UIScrollView添加为子视图,然后将textviews添加到scrollview是否会更好。

I might be out in left field here, but I've never used a scrollview directly at the container-view level, and I wonder if that might be the problem. 我可能在这里位于左侧,但是我从未在容器视图级别直接使用过滚动视图,我想知道这是否可能是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM