使用loadView创建视图时,视图的帧大小不会更改 [英] The frame size of the view is not changing on rotation when the view is created with loadView

查看:205
本文介绍了使用loadView创建视图时,视图的帧大小不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有没有xib的UIViewController,我使用loadView构建我的UI,创建并添加两个滚动视图。问题是,当旋转发生时,主视图框架大小不会改变。我的意思是,我在loadView中为主视图设置初始帧大小(纵向模式:帧大小宽度= 320,高度= 480)。旋转到横向方向后,视图主框架尺寸不会更改,并且与纵向模式相同。这是我的loadView:

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];

我还为视图设置了autoresizingMask以扩展或缩小以适应屏幕。但是在调试控制台时我得到相同的宽度和高度值。我需要获得一个新的大小,因为我需要在旋转发生时重新定位我的两个scrollViews,例如缩小高度并在scrollViews上将旋转宽度扩展到横向模式。我可以在shouldRotate中手动完成,只是好奇它应该如何以正确的方式完成。

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.

推荐答案

我注意到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.

这篇关于使用loadView创建视图时,视图的帧大小不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆