【问题标题】:Autoresize UIScrollView自动调整 UIScrollView 的大小
【发布时间】:2011-03-03 14:42:49
【问题描述】:

我制作了一个 UIViewController,它以编程方式生成一个 UIScrollView。一切都很好,但是当我旋转设备时,UIScollView 应该调整大小,使其占据我的视图的完整宽度。

有没有办法在不重建完整 UIScrollView 的情况下做到这一点?

非常感谢! 塞巴斯蒂安

这在我的 viewDidLoad 中被调用:

    -(void)buildmyScroller {
    myScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 800, 768, 100)];


    //...adding some subviews to myScroller


    thumbScroller.contentSize = CGSizeMake(3000, 100);
    [[self view] addSubview:myScroller];
}

然后我尝试用这个来调整myScroller的大小,当我使用setFrame时,我说myScroller不会响应它......:

-(void)changemyScroller {
        UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
    if (interfaceOrientation == UIInterfaceOrientationPortrait) {
        [thumbScroller setFrame:CGRectMake(0, 805, 768, 150)];
    }
    else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        thumbScroller.frame = CGRectMake(0, 805, 768, 150);
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        thumbScroller.frame = CGRectMake(0, 549, 1024, 150);
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
        thumbScroller.frame = CGRectMake(0, 549, 1024, 150);
    }
}

并在 didAnimateFirstHalf 中调用了该方法...因为我不知道在哪里调用它。

再次感谢!

【问题讨论】:

    标签: iphone uiscrollview autoresize


    【解决方案1】:

    [scrollView setFrame:CGRectmake(x, y, width, height)]; //也许你需要对scrollView的内容做同样的事情,让它适合你的布局

    应该这样做。如果需要转换,您可以将其包装在 UIAnimation 块中。

    【讨论】:

    • 非常感谢!解决方案可以如此简单:)
    • 还有一个问题,我的滚动条现在适合我的布局,内容也有合适的大小,但是当我从横向(滚动宽度 1024 像素)开始旋转 zu 纵向(滚动宽度 768 像素)时,我无法使用滚动条.当我从纵向开始旋转到横向时,我只能在前 768px 进行交互,所以在右侧我无法与滚动条交互。当我转到另一个视图并返回到包含滚动视图的视图时,我可以使用它。所以不能在显示时更改 scrollView 吗?或者我应该在哪里改变它?谢谢。
    • 您好,我猜您在 loadView 或 viewDidLoad 中设置了 UIScrollView?也许甚至初始化? (这意味着它仅在调用这些函数时更新)。尝试将 UIScrollView 的实例化放在 viewDidLoad 中,但将内容和方向的设置、更改放在单独的函数中。现在您可以从 viewWillAppear 和 ShouldAutoRotate... 调用此函数...因此,当您第一次加载它时、旋转时以及转到另一个视图并返回时都会发生设置。
    • 无法完成,要么构建多个滚动条,要么不更新滚动条。我可以给你发一封包含一些代码的邮件吗,我很沮丧;(
    • 嗨。尝试将您的 viewController 代码添加到您的第一篇文章中。尤其是 init 方法、viewDidLoad、loadView 和 shouldAutorotate。你可以给我发邮件,但关键是其他人应该从我们的想法中受益:)
    【解决方案2】:

    试试这个:

        if(self.rowNumber == 0){
        /******************* Scroller Setup *****************/
        // how many pages
        int pageCount = 5;
        //set up the scrollView
        UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 960)];
        // support for Landscape Orienation
        if(UIInterfaceOrientationLandscapeLeft){
            [scroller  setFrame:CGRectMake(0,0,1024, 704)];
        }
        if(UIInterfaceOrientationLandscapeRight){
            [scroller  setFrame:CGRectMake(0,0,1024, 704)];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2016-07-15
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多