Objective c 具有多个视图的iPad应用程序,如何使一个视图仅在横向视图中可见?
在得到miamk的支持性回答后,我离实现我想要的目标又近了一步。。但现在有以下问题: 我有一个iPad应用程序,可以在所有四种视图模式下使用(纵向向上/向下和横向向左/向右)。但在某一点上,我有一个观点,我只想在横向模式下被看到。因此,我在UIViewController中执行以下操作,以触发查看仅横向视图的操作:Objective c 具有多个视图的iPad应用程序,如何使一个视图仅在横向视图中可见?,objective-c,xcode,ipad,orientation,landscape,Objective C,Xcode,Ipad,Orientation,Landscape,在得到miamk的支持性回答后,我离实现我想要的目标又近了一步。。但现在有以下问题: 我有一个iPad应用程序,可以在所有四种视图模式下使用(纵向向上/向下和横向向左/向右)。但在某一点上,我有一个观点,我只想在横向模式下被看到。因此,我在UIViewController中执行以下操作,以触发查看仅横向视图的操作: - (void) showProperty:(Property *) property { if ([self interfaceOrientation] == UIInte
- (void) showProperty:(Property *) property {
if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft || [self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) {
PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:@"PropertyViewController" bundle:[NSBundle mainBundle]];
propertyView.property = property;
[self.navigationController pushViewController:propertyView animated:YES];
[propertyView release];
propertyView = nil;
}
else {
RotateDeviceViewController *rotateView = [[RotateDeviceViewController alloc] initWithNibName:@"TabRotate" bundle: [NSBundle mainBundle]];
rotateView.property = property;
[self.navigationController pushViewController:rotateView animated:YES];
[rotateView release];
rotateView = nil;
}
}
它工作正常,因此当iPad处于横向模式时,会显示所需的屏幕(PropertyViewController),如果不是,则会显示RotatedDeviceController,该控制器会向用户显示一条消息,说明他/她应该旋转设备以正确查看屏幕
那就行了
然后,此旋转设备控制器出现问题。。我有以下几点:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
[self showProperty];
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
- (void) showProperty {
PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:@"PropertyViewController" bundle:[NSBundle mainBundle]];
propertyView.property = property;
[self.navigationController pushViewController:propertyView animated:YES];
[propertyView release];
}
因此,只要我将设备旋转到横向模式(查看旋转设备控制器),我就会向用户显示属性视图控制器。这很有效。。。但是当属性视图控制器出现时,它显示我的布局旋转了90度。因此,它基本上是以纵向模式显示内容,而不是使用横向模式(这实际上是您握住设备的方式)
我希望这是有道理的,有人能告诉我是什么原因造成的。试试这个:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
希望这能有所帮助。对于@MacN00b的答案来说,一个更优雅的解决方法(至少在设计方面)是设置一个纵向视图,其中包含一条消息,告诉用户应该旋转设备,只有当用户旋转设备时,才会显示为横向构建的视图
老实说,我认为当用户仍处于纵向时,所有东西都已经旋转了,这很难看。您可以使用以下方法收听方向的变化:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
然后通过加载适当的视图来响应这些请求
- (void) orientationChanged:(id)object
{
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portraitView;
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view = self.landscapeView;
}
}
如果这是问题屏幕的专用UIView子类,则可以使肖像视图包含一个标签,通知用户旋转屏幕以查看内容,然后使横向视图包含实际内容
我目前在一个应用程序中这样做,两个视图都包含在一个nib中。请确保在IB中为每个视图适当地设置视图属性的方向…如何交换视图?这可能与:-1有关。Jules明确指出重写这个方法并不能提供他所要求的解决方案。事实上,正如我所说的,这与我在代码中所做的完全相同,就像我上面所说的。但这只会阻止视图旋转到纵向模式。但是如果查看器已经处于纵向模式,它不会旋转视图。+1是的,您是对的。这将是一个更好的解决办法。那么,我该如何为此设置两种不同的视图呢?一个用肖像画,一个用风景画。我如何实现这一点?=/(我想我也会在谷歌上找到)UIViewController有一个名为
interfaceOrientation
的只读属性。您可以编写一个if条件来加载一个纵向视图和另一个横向视图。谢谢。这真的很有帮助,但现在我有另一个问题。我相应地更新了我的问题。希望你能帮助我!