Objective c iPad/iPhone定位问题

Objective c iPad/iPhone定位问题,objective-c,ios,interface-builder,orientation,xcode4.3,Objective C,Ios,Interface Builder,Orientation,Xcode4.3,我意识到与这个问题相关的问题很多,但是我还没有找到一个能解决我的问题 首先,我在菜单中设置了此代码。h -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } 状态栏会随方向变化,但视图不会旋转或调整大小。为了缩小我的问题范围,我决定尝试在一个基于方向的.xib中切换两个视图 - (void)viewDidLoad {

我意识到与这个问题相关的问题很多,但是我还没有找到一个能解决我的问题

首先,我在菜单中设置了此代码。h

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    
return YES; }
状态栏会随方向变化,但视图不会旋转或调整大小。为了缩小我的问题范围,我决定尝试在一个基于方向的.xib中切换两个视图

    - (void)viewDidLoad {
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
     }
    -(void) orientationChanged:(NSNotification *)object
{
   UIDeviceOrientation deviceOrientation = [[object object] orientation];

    if (deviceOrientation == UIDeviceOrientationLandscapeLeft || deviceOrientation == UIDeviceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
    else 
    {
        self.view = self.portraitView;
    }
}
在iOS模拟器中,视图肯定会更改为特定视图。然而,景观视图显示为纵向和横向

IB中的我的风景

更改方向后iOS模拟器中的我的横向视图

我做错了什么?我想不出来,任何帮助都将不胜感激。先谢谢你。 **编辑:下面是新代码** 可以我的问题是,视图在横向视图中正确加载,只是横向加载。因此,景观视图是横向加载的。我根据@Seega修订的代码是:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

    [[TTNavigator navigator] setCustomTarget:self];
    [[TTNavigator navigator] setCustomAction:@selector(photoAction)];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
    else 
    {
        self.view = self.portraitView;
    }
}

-(void) orientationChanged:(NSNotification *)object
{
    UIDeviceOrientation deviceOrientation = [[object object] orientation];

    if (deviceOrientation == UIDeviceOrientationLandscapeLeft || deviceOrientation == UIDeviceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
    else 
    {
        self.view = self.portraitView;
    }
}

你最好简单地使用它

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
 if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
    else 
    {
        self.view = self.portraitView;
    }
}
来处理旋转,而不是创建自己的旋转。
现在,您可以删除所有通知内容。

您是否可以确保已按以下方式选择了所有定向支持。我试过这个,它似乎对我很有效


我建议您将代码放入:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
而不是制定新的方法。如果shouldAutorotateToInterfaceOrientation方法为该旋转返回YES,则将自动调用该函数

UIDeviceOrientation和UIInterfaceOrientation之间也存在差异,因此请确保引用的是正确的。您的现有代码将更改为以下代码:

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    self.view = self.landscapeView;
}
else 
{
    self.view = self.portraitView;
}
您还可以使用宏来检查接口方向,以保持代码的整洁

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) 
{
    self.view = self.landscapeView;
}
else 
{
    self.view = self.portraitView;
}
想法

如果要在interface builder中设置视图,然后切换视图旋转时显示的实际视图,则需要以该特定方向构建该视图,而不仅仅是调整其大小以适应该方向

要检查此项,请执行以下操作:

在Interface Builder中打开.xib。 单击“对象”下的“视图”,以便选择整个视图。 查看IB右侧的“模拟指标”下。确保在“方向”下拉列表中选择了“横向”。 如果您的视图为您想要表示景观视图的视图显示“纵向”,那么可能是xcode将您的景观视图旋转为纵向视图,并干扰了您的演示


让我知道这是否有用

嘿,谢谢大家的帮助。我有一位朋友帮忙,但我不完全确定问题出在哪里。我可以告诉你我知道他做过的事

暂时禁用我们的广告

完全删除了第二个视图,并使用本机定向和调整大小

将此添加到几乎每个.m文件中

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    
return YES;
}

以及一个以编程方式更改了一些与此类似的其他视图

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
     if (interfaceOrientation == UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight)
     {
          if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) 
          {         
               self.Button.frame = CGRectMake(27,96,86,86);
               self.Label.frame  = CGRectMake(27,162,86,21);
          }
         else 
         {
               self.Button.frame = CGRectMake(18,100,86,86);
               self.Label.frame  = CGRectMake(18,164,86,21);
          }
     }
 } 

很抱歉,我无法完全解释答案,我不完全理解项目中影响方向的所有内容。我对iOS开发还是很陌生。

是的,所有支持的设备定向框都被选中。-voidwillRotateToInterfaceOrientation:UIInterfaceOrientationInterfaceOrientationDuration:NSTimeIntervalduration{[[UIDevice currentDevice]BegineratingDeviceOrientationNotifications];[[NSNotificationCenter defaultCenter]addObserver:self selector:@selectororientationChanged:name:@UIDeviceOrientationDidChangeNotification对象:nil];}这就是您的想法吗?这对我不管用。感谢您的帮助上述代码的问题是UIDeviceOrientation和UIInterfaceOrientation不同,因此您正在检查toInterfaceOrientation==UIDeviceOrientation,它们永远不会。这里有更多关于差异的信息-这是一个超级简单的打字错误,不会引起任何警告/错误,所以它更狡猾。以前从未有过,所以没有考虑过。但是现在你的问题来了;我应该使用UIDeviceOrientation还是UIInterfaceOrientation?您应该使用UIInterfaceOrientation,因为如果您的应用程序仅在纵向模式下工作,并且用户将其设备旋转到横向模式,那么现在该应用程序看起来仍然像它的纵向模式,只是被侧放,当您检查UIDeviceOrientation时,它将处于横向,因为设备已旋转,但当您检查UIInterfaceOrientation时,它将显示为纵向,因为应用程序本身未旋转。这有意义吗?还有更多细节->这很有意义,谢谢。但是,如何设置应用程序随设备旋转?好问题。我想不出是否要将应用程序设置为旋转,您需要1在应用程序的“摘要”选项卡下选择“支持的设备方向”,查看Gopal Nair在此页面上发布的图像,2您需要返回“是”;在-BOOLshouldAutorotateToInterfaceOrientation:UIInterfaceOrientationinterfaceOrientation方法中,用于要为该ViewController支持的任何方向注意:每个v
iew控制器可以支持不同的设备方向。再次感谢您的持续帮助。非常感谢。肖像视图设置为portait,景观视图设置为landscape。该死,我以为我有什么。删除所有[[UIDevice currentDevice]BegingeratingDeviceOrientationNotifications];[[NSNotificationCenter defaultCenter]addObserver:自选择器:@selectororientationChanged:名称:@UIDeviceOrientationIDChangeNotification对象:nil];stuff和complete-void-orientationChanged:NSNotification*对象函数。使用UIInterfaceOrientationAndscapeLeft和UIInterfaceOrientationAndscapeRight Additional检查函数是否按预期工作更改if语句中的颜色,而不是设置另一个UIView。我这方面运气不好。我按照说明设置了它,并在orientationChanged函数中输入了断点,它不会被调用。删除该死的orientationChanged:你不需要它!!!!只需使用-voidwillRotateToInterfaceOrientation:UIInterfaceOrientationInterfaceOrientationDuration:NSTimeIntervalduration{if to InterfaceOrientation==UIInterfaceOrientationAndScapeLeft | | to InterfaceOrientation==UIInterfaceOrientationAndScapeRight{self.view=self.landscapeView;}否则{self.view=self.graphicalview;}}这不起作用。以前,视图至少改变了,现在它不工作了。