UIView在风景中有纵向尺寸?

时间:2012-07-09 12:22:43

标签: objective-c ios cocoa ipad

我有一个包含1 UIViewController的故事板,其中包含1个UIView,其中包含许多嵌套的UIView。我将View Controller子类化为实现此方法:

- (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中我这样做:

PASectionView* sectionView = [[PASectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 180)];
[self addSubview:sectionView];

问题是控件只有756像素宽而不是预期的1024元。有关详细信息,请参阅下面的屏幕截图。 enter image description here我一直在网上搜索,但无法在任何地方找到这个令人沮丧的问题的解决方案。我使用Xcode 4.5和iOS5.1设置为基本SDK。

修改 它通过用边界替换框架来工作。但是,我不了解发生了什么,所以它不适用于帧大小。

4 个答案:

答案 0 :(得分:11)

  

框架矩形,用于描述视图的位置和大小   超级视图的坐标系

     

@property(非原子)CGRect框架

  

边界矩形,用于描述视图的位置和大小   它自己的坐标系

     

@property(非原子)CGRect界限

使用边界,而不是框架。

答案 1 :(得分:2)

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

[myView setAutoresizingMask:UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleRightMargin];

答案 2 :(得分: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);    
}

答案 3 :(得分:0)

您必须为子视图设置自动调整大小屏幕

如果通过代码添加了subView,则在将subView添加到superview之前添加此行代码。     yourSubView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

否则,如果在nib文件中添加了子视图,请在nib文件中选择子视图磁体并调整子视图属性中的大小。

相关问题