在iOS上,对于单一视图应用程序,视图是否始终布局为纵向,并调整大小,即使应用程序仅在横向模式下运行?

时间:2012-09-01 00:11:21

标签: ios uiviewcontroller uiinterfaceorientation

如果应用仅支持横向模式,请单击“项目摘要”中的图标和

中的图标
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return  interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
                   interfaceOrientation == UIInterfaceOrientationLandscapeRight;

}

然后如果在viewDidLoad,我做

UIView *smallBox = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 300, 300)];
smallBox.autoresizingMask = UIViewAutoresizingFlexibleWidth;

smallBox.backgroundColor = [UIColor yellowColor];
[self.view addSubview:smallBox];

然后我在横向方向上拿着一个物理iPad 2,然后从Xcode运行该程序。黄色框实际上比300px宽得多。但这只能在纵向模式下首次将300px视为300px,然后调整为固定的左右边距,使其宽度为556px。 (在我添加的触摸处理程序中确认打印出框架宽度)。

但该应用程序配置为仅在横向模式下运行,那么为什么要调整大小?因此,似乎iOS应用程序的规则是 始终 首先在纵向模式下布局,然后在需要横向模式时调整大小/重新布局?

1 个答案:

答案 0 :(得分:-2)

尝试将此方法设置为自动化界面:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation != UIInterfaceOrientationPortrait);

}