2012-07-09 70 views
5

我有一个故事板1 UIViewController,持有1 UIView,其中包含一些嵌套UIView s。我子类的视图控制器实现此方法:UIView在横向上有纵向尺寸吗?

- (BOOL)shouldAutorotateToInterfaceOrientation: UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 

我还添加了

<key>UISupportedInterfaceOrientations</key> 
    <array> 
    <string>UIInterfaceOrientationLandscapeLeft</string> 
    <string>UIInterfaceOrientationLandscapeRight</string> 
    </array> 
<key>UIInterfaceOrientation</key> 
<string>UIInterfaceOrientationLandscapeLeft</string> 

Info.plist

在主要的UIView的viewDidLoad中我这样做:

的问题是控制仅仅是756px宽,而不是预期的1024参见下面的截图了解详情。 enter image description here我一直在网上搜索,但我无法找到解决这个令人沮丧的问题的任何地方。我使用Xcode 4.5和iOS5.1作为基础SDK。

编辑 它的工作方式是用边界替换帧。但我不明白发生了什么,所以它不适用于帧大小。

回答

11

框架矩形,其描述了在 它的父的坐标系视图的位置和大小

@属性(非原子)的CGRect帧

边界矩形,其描述了在 其自己的视图的位置和大小坐标系

@属性(非原子)的CGRect界定

使用范围,不是帧。

+0

谢谢!用界限代替“框架”是可行的!但是,我不明白为什么?任何机会使它与框架一起工作?不明白为什么UIView的大小在超视图和它自己的坐标系统中有所不同 – s1m0n 2012-07-09 16:14:03

+0

根据我的理解,这是因为视图位于窗口上,并且该窗口必须具有内部横向模式,如果这种模式在某种程度上接近于如果窗口与其视图一起旋转并且它是子视图,则窗口坐标系中的视图坐标不反映旋转。你可能想要刷新坐标系知识以便更好地理解(旋转和可能的转换必须足够用于这种情况):http://en.wikipedia.org/wiki/Cartesian_coordinate_system这是我记得的一个学校数学。 – 2012-07-09 22:53:54

+1

这可能是值得通过http://stackoverflow.com/questions/1210047/iphone-development-whats-the-difference-between-the-frame-and-the-bounds,和一些相关的问题。 – 2012-07-10 15:01:50

2

autoresizingMask设置为您希望在旋转时自动调整大小的任何视图。就像这样:

[myView setAutoresizingMask:UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleRightMargin]; 
+1

感谢您的回答。但我不想让我的应用自动旋转或什么。风景模式对我来说已经足够了。所以它不应该自动旋转或不管,我只是想让它返回正确的横向尺寸。 – s1m0n 2012-07-09 12:45:21

0

问题是因为加载到风景模式之前,你的代码行调用该方法装载 在该方法是在调用此[PASectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 180)]; [self addSubview:sectionView];如果它是视图控制器类,那么它应该,如果它是[PASectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)]; [self addSubview:sectionView];UIView的子类,那么你从哪里得到了ViewDidLoad方法。 现在要解决你的问题 在.h类: 写这个 PASectionView * sectionView; 在.m类实现此方法

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) 
{ 

} 
else // OrientationLandscape 
{ 
sectionView.frame = CGRect(sectionView.frame.origin.x,sectionView.frame.origin.y,self.view.frame.size.width,180);  
} 
0

必须设置自动调整大小的面膜为你的子视图,

添加子视图的上海华,如果你的子视图是由代码添加之前添加这行代码。 yourSubView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

否则,如果您的子视图添加到nib文件中,请在nib文件的子视图属性中选择边框磁盘并调整大小掩码。