gpt4 book ai didi

objective-c - iOS/Objective-C : Creating a UIScrollView which fills screen and reorients

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:34:29 25 4
gpt4 key购买 nike

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

事实证明这非常具有挑战性 - ScrollView 似乎不愿意在横向 View 中正确调整大小,我不确定为什么。

我决定忘记使用应用程序框架并简单地尝试填充屏幕,忽略状态栏高度,但即使这样也是不可能的。

您可以在下面看到我使用的方法。有人会好心地演示如何正确地做到这一点吗?我似乎一直遇到正确调整对象大小的问题,尤其是在尝试完全填满 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
}

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

最佳答案

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

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

这将确保 ScrollView 将始终与其容器 View 一起调整大小。

关于objective-c - iOS/Objective-C : Creating a UIScrollView which fills screen and reorients,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5692845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com