gpt4 book ai didi

objective-c - 方向改变时的 UIScrollview 内容大小

转载 作者:可可西里 更新时间:2023-11-01 05:23:17 24 4
gpt4 key购买 nike

我有一个带分页的 ScrollView 。在 viewDidLoad 中,我检查当前方向是否为横向,然后我将其内容大小的高度设置为 440

 if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) 
{
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages,340)];


}
else if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))

{
[scroll setFrame:CGRectMake(0,0,480,480)];
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 440)];


}

一切正常,scrollview 滚动流畅,没有对角线滚动。

但是当方向改变时,

我必须再次设置 scrollview 的 frame 和 contentsize,我设置如下

-(void)orientationChanged:(id)object
{
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
self.scroll.frame = [[UIScreen mainScreen]bounds];

[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 340)];
}


else
{
self.scroll.frame = CGRectMake(0,0,480,480);
[scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 600)];
}


}

我不明白为什么我必须在横向模式下将内容大小的高度设置为 600,这也不够。它又增加了一个问题,即 scrollview 开始沿对角线滚动,这是我不想要的,因为它看起来很奇怪。谁能帮助我了解我在哪里以及缺少什么?

我已将 scrollview 的自动调整掩码设置为

[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];

但改变它没有帮助。

最佳答案

  1. 不要使用 UIDeviceOrientation。请改用 UIInterfaceOrientationDeviceOrientation 有两个您在这里不需要的额外选项。 (UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown)

  2. shouldAutorotateToInterfaceOrientation

  3. 返回 Yes
  4. 现在 willRotateToInterfaceOrientation: duration: 将在您每次旋转设备时调用。

  5. 像这样实现这个方法。

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    CGRect frame;
    int pageNumber = 2;
    int statusBarHeight = 20;

    if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
    frame = CGRectMake(0, 0, 480, 320 - statusBarHeight);
    } else {
    frame = CGRectMake(0, 0, 320, 480 - statusBarHeight);
    }

    scrollView.frame = frame;
    scrollView.contentSize = CGSizeMake(frame.size.width * 2, frame.size.height);
    }

    让,

    页数 = 2

    statusBarHeight = 20

关于objective-c - 方向改变时的 UIScrollview 内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13430379/

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