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

发布于 2024-11-01 20:46:33 字数 1273 浏览 0 评论 0原文

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

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

I'm trying to achieve the simplest thing - creating a UIScrollView which fills (and exactly fits) the application frame (i.e. screen minus status bar height) regardless of device orientation.

This is proving very challenging though - the scroll view seems reluctant to resize correctly in landscape view, and I'm not sure why.

I decided to forget about using the application frame and simply attempt to fill the screen, ignoring status bar height, yet even this hasn't been possible.

You can see the approach I'm using below. Would anyone be kind enough to demonstrate how to do this correctly? I seem to permanently run into issues with sizing objects correctly, especially when attempting to fill the iPhone/iPad screen entirely.

- (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
}

Your advice is most welcome, many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2024-11-08 20:46:33

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

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

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

I would avoid trying to determine the size yourself; just let iOS do the work for you:

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

This will ensure that the scroll view will always be resized along with its container view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文