【问题标题】:iOS/Objective-C: Creating a UIScrollView which fills screen and reorientsiOS/Objective-C:创建一个填充屏幕并重新定向的 UIScrollView
【发布时间】:2011-08-07 06:18:37
【问题描述】:

我正在尝试实现最简单的事情 - 创建一个 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
}

非常欢迎您的建议,非常感谢。

【问题讨论】:

  • 要获取您的视图能够使用的框架,您可以使用:[[UIScreen mainScreen] applicationFrame]

标签: objective-c ios uiscrollview resize fill


【解决方案1】:

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

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

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

【讨论】:

  • 非常感谢 odrm,这可以解决问题 :) 如果您不介意,我有一个后续问题:在指定滚动视图的内容大小时,我是否应该设置滚动子视图也是那个维度?我正在以编程方式而不是使用 Interface Builder 来布置视图,并且还没有在网上找到合适的示例。我应该补充一点,我正在滚动的子视图不包含 UIKit 组件,而是有一个用于手动绘图的自定义 drawRect 实现。这就是为什么我不确定是否设置它的边界/框架..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多