当使用 loadView 创建视图时,视图的框架大小在旋转时不会改变
我有没有 xib 的 UIViewController,并且我正在使用 loadView 来构建创建并添加两个滚动视图的 UI。问题是,当旋转发生时,主视图框架的大小不会改变。我的意思是,我在 loadView 中设置主视图的初始帧大小(纵向模式:帧大小宽度 = 320,高度 = 480)。旋转到横向后,视图主框架尺寸没有变化,与纵向模式下相同。这是我的 loadView:
-(void)loadView{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)] autorelease];
self.view.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.scrollView1 = [[[UIScrollView alloc] init] autorelease];
self.scrollView1.frame = CGRectMake...
[self.view addSubview:self.scrollView1];
self.scrollView2 = [[[UIScrollView alloc] init] autorelease];
self.scrollView2.frame = CGRectMake...
[self.view addSubview:self.scrollView2];
我还为视图设置了 autoresizingMask 来扩大或缩小以适应屏幕。但是在控制台中调试时我得到了相同的宽度和高度值。我需要获得新的尺寸,因为在旋转发生时我需要重新定位两个滚动视图,例如在旋转到横向模式时缩小滚动视图的高度并扩大滚动视图的宽度。我可以在 shouldRotate 中手动执行,只是好奇应该如何以正确的方式完成它。
I have UIViewController without a xib, and I'm using loadView to build my UI that creates and adds two scroll views. The thing is, the main view frame size is not changing when rotation happens. I mean, I'm setting initial frame size for the main view in loadView (portrait mode: frame size width = 320, height = 480). After rotation to landscape orientation, the view main frame size isn't changing and is the same as in portrait mode. Here's my loadView:
-(void)loadView{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)] autorelease];
self.view.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.scrollView1 = [[[UIScrollView alloc] init] autorelease];
self.scrollView1.frame = CGRectMake...
[self.view addSubview:self.scrollView1];
self.scrollView2 = [[[UIScrollView alloc] init] autorelease];
self.scrollView2.frame = CGRectMake...
[self.view addSubview:self.scrollView2];
I have also set autoresizingMask for the view to expand or shrink to fit the screen. But I'm getting the same width and height values when debugging that in console. I need to get a new size because I need to reposition my two scrollViews when rotation happens, for example to shrink the height and expand the width on the scrollViews on rotation to landscape mode. I could do manually in shouldRotate, just curious about how it should be done in proper way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到 self.view.frame 大小不会改变,但 self.view.bounds 会旋转,并且边界表示相对于当前界面方向的正确值。
I have noticed that self.view.frame size does not change but self.view.bounds does on rotation, and bounds represent correct values with respect to current interface orientation.