简体   繁体   中英

iOS textviews not expanding in scrollview in landscape

I had a view with some textviews in it that were working as I wanted. 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.

I've checked and my scrollview is set to autoresize subviews.

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. 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. So you're forcing the scrollView to maintain portrait dimensions in landscape.

Keep your autoresizeMask routine and discard your willRotateToInterfaceOrientation routine. Does it work now?

If not, try putting the willRotate code into *did*RotateToInterfaceOrientation.


Another idea:

I notice that in viewDidLoad you cast the container view ( self.view ) to a 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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