iOS/Objective-C:创建一个填充屏幕和重新定位的UIScrollView

Kom*_*ave 2 resize objective-c fill uiscrollview ios

我正在尝试实现最简单的事情 - 创建一个UIScrollView,它可以填充(并且完全适合)应用程序框架(即屏幕减去状态栏高度),而不管设备方向如何.

这证明非常具有挑战性 - 滚动视图似乎不愿意在横向视图中正确调整大小,我不确定为什么.

我决定忘记使用应用程序框架,只是尝试填充屏幕,忽略状态栏高度,但即使这是不可能的.

你可以在下面看到我正在使用的方法.有人会善意地证明如何正确地做到这一点吗?我似乎永远遇到了正确调整对象大小的问题,特别是在尝试完全填充iPhone/iPad屏幕时.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    CGFloat viewedWidth, viewedHeight;
    const CGSize dim = [UIScreen mainScreen].bounds.size;
    if (UIInterfaceOrientationIsPortrait([UIDevice currentDevice].orientation)) {
        viewedWidth = dim.width;
        viewedHeight = dim.height;
    } else {
        viewedWidth = dim.height;
        viewedHeight = dim.width;
    }
    [self.scrollView setFrame:CGRectMake(0.0f, 0.0f, viewedWidth, viewedHeight)];
}

-(void)viewDidLoad {
    [self.view setFrame:[UIScreen mainScreen].bounds];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,1,1)];
    [self.scrollView setDelegate:self];        
    [self.view addSubview:self.scrollView];
    [self.scrollView setContentSize:sizeManager.canvasSize];
    [self didRotateFromInterfaceOrientation:UIInterfaceOrientationPortrait]; // NOTE: orientation passed is not used, dummy
}
Run Code Online (Sandbox Code Playgroud)

非常感谢您的建议,非常感谢.

odr*_*drm 5

我会避免试图自己确定尺寸; 让iOS为您完成工作:

- (void)viewDidLoad {
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:sv];
    [sv release];
}
Run Code Online (Sandbox Code Playgroud)

这将确保滚动视图始终与其容器视图一起调整大小.