方向更改时的UIScrollview大小

时间:2012-01-31 23:52:18

标签: iphone ios uiscrollview

当我在滚动视图中查看图像时,当我更改手机的方向时,滚动视图不会改变手机的方向。

我想知道是否有人可以向我解释如何实现这一目标。

这些是我的界面构建器

中的设置

enter image description here

我刚刚做了一些研究,现在有第一个选择工作,但是如果我尝试另一张图片它可以工作..而且我不确定为什么...继承我的代码

这是半工作的原因

scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 


- (void)loadImage:(NSString *)myImageName
{
//    //Create scrollview

    scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 
    scrollView.delegate = self;

    //Create scrollviewimage
    if ([myImageName isEqualToString:@"one"]) {
        image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hyundai.png"]];
        containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, 550)];
        NSLog(@"1");
    }
    if ([myImageName isEqualToString:@"two"]) {
        image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"keso.png"]];
        containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 520)];
        NSLog(@"2");
    }
    if ([myImageName isEqualToString:@"three"]) {
        image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"conforti.png"]];
        containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 700)];
        NSLog(@"3");
    }

    //Add subview
    [containerView addSubview:image];
    //initViews
//    scrollView.contentSize = containerView.frame.size;
    [scrollView addSubview:containerView];

    //scrolling
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 31.0;
    scrollView.BackgroundColor = UIColor.whiteColor;
    [scrollView setZoomScale:scrollView.minimumZoomScale];

    //highrachy
    [self.view addSubview:scrollView];
    scrollView = nil;
}

编辑:对@DJ的回应

这是输出

2012-02-01 13:30:35.683 Diagram Test[1672:707] The view controller <UINavigationController: 0x14cdd0> returned NO from -shouldAutorotateToInterfaceOrientation: for all interface orientations. It should support at least one orientation.
2012-02-01 13:30:44.182 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:30:47.097 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:30:47.158 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:30:51.509 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:30:51.555 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:30:55.473 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:30:55.519 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:30:59.293 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:30:59.352 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:31:02.237 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:31:02.281 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:31:03.814 Diagram Test[1672:707] height = (null)i, width = (null)i
2012-02-01 13:31:03.858 Diagram Test[1672:707] height = (null)i, width = (null)i

3 个答案:

答案 0 :(得分:2)

旋转scrollview会调整边界的大小,但不会调整不受自动调整大小影响的contentSize。

contentSize是滚动视图的可滚动区域,因此您需要在旋转期间手动调整大小。这可能导致一系列其他调整,因此请注意,旋转滚动视图可能有点棘手。

例如,如果要滚动图像列表,则还需要重新对齐滚动位置,并可能修改缩放设置以匹配新的宽度和高度。

如果您只是放大单个图像,则任务更容易,因为只需要调整缩放(如果有的话)。

另一个提示:使用contentSize进行播放而不旋转,这样您就可以看到它如何影响滚动和可见区域。

答案 1 :(得分:0)

这些是正确的弹簧和支柱,使滚动视图在方向更改时填充父视图。问题很可能是scrollview内容无法适应。

如果需要保持图像居中等,还可以在willRotateToInterfaceOrientation:方法中修改scrollview.contentSize和scrollView.contentOffest。

答案 2 :(得分:0)

我感觉什么都没有旋转..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     NSLog(@"height = %i, width = %i",scrollviewname.bounds.size.height, scrollviewname.bounds.size.width);
     return true;
}

我从内存中键入了该代码..所以如果有拼写错误抱歉..请检查这些日志以确保uiscrollview首先旋转。