2013-01-11 43 views
0

问题1:如何获得我的UIView的正确大小?

如何获取UIView的正确大小?

我正在创建CGRect以显示使用平铺图层的一些图像。 当我创建CGRect时,我基本上需要它与我的UIView具有完全相同的大小。这原来是很难.. 当我NSLog()我的mainView.bounds.size.width或我的mainView.frame.size.width他们总是错误的,当在风景!尽管我可以看到实际的视图更宽,但他们总是将这些值注释为纵向。扭转它们也是错误的。 将横向设置为高度并不够好,反之亦然,我需要正确的值。

我已经能够使它看起来正确的是手动将在1024宽度时,在景观的唯一途径,而这并不总是工作,要么,因为:

问题2:

什么是正确的方法来检查设备是否在横向或不横向?

我一直在使用

if([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) 

但是这并不总是工作。如果我在启动时将设备放在横向模式下,这是正确的,但是如果我将其设置为横向,然后将iPad平放在仪表板上并将其放置在横向位置,然后启动它,然后显示横向飞溅,但该代码认为它是肖像的。 代码不为iPad模拟器要么..

编辑

出于某种原因,在所有的工作,当我决定增加对横向的支持,这是不够的,只是查景观-orientations目标的摘要页,我不得不实际子类的我的TabBarController和身体告诉它与

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

旋转我不应该这样做..好吗?如果我创建一个像这样的空项目,它不需要它..我不知道为什么。

+0

我记得有问题,就是这样的,当我把一个标签栏控制器,导航控制器内(不支持的配置苹果公司说,不执行)。这是你正在做什么的机会? – lnafziger

+0

@lnafziger不,但有些时候我在tabBarController里面有navigationControllers,但是这也不应该是问题.. – Sti

+0

是的,这是完全可以接受的....也许你可以上传你的应用程序,或者更好的一个小版本您的应用程序展示了这种行为,以便我们可以更仔细地查看它。 – lnafziger

回答

1

问题1: 是的,没错。 :)

问题2: 我刚回家并检查了我自己的代码。我用:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

检查当前接口的方向。然后,您可以使用类似:

if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { 
    // Do something 
} else if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){ 
    // Do something else 
} 

无论其你真的不应该需要做到这一点,如果你妥善处理旋转事件。

这是我处理旋转的典型方式,当我需要根据方向来调整代码UI元素的位置:

#pragma mark - View rotation methods 

// Maintain pre-iOS 6 support: 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

// Make sure that our subviews get moved on launch: 
- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    [self moveSubviewsToOrientation:orientation duration:0.0]; 
} 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    [self moveSubviewsToOrientation:toInterfaceOrientation duration:duration]; 
} 

// Animate the movements 
- (void)moveSubviewsToOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration 
{ 
    [UIView animateWithDuration:duration 
        animations:^{ 
         [self.tableView reloadData]; 
         if (UIInterfaceOrientationIsPortrait(orientation)) 
         { 
          [self moveSubviewsToPortrait]; 
         } 
         else 
         { 
          [self moveSubviewsToLandscape]; 
         } 
        } 
        completion:NULL]; 
} 

- (void)moveSubviewsToPortrait 
{ 
    // Set the frames/etc for portrait presentation 
    self.logoImageView.frame = CGRectMake(229.0, 21.0, 309.0, 55.0); 
} 

- (void)moveSubviewsToLandscape 
{ 
    // Set the frames/etc for landscape presentation 
    self.logoImageView.frame = CGRectMake(88.0, 21.0, 309.0, 55.0); 
} 

我也把moveSubviewsToOrientationviewWillAppear将它旋转

+0

基本上它从viewDidLoad被调用,但是当我在横向启动应用程序时,这不是无关紧要吗?或者,应用程序是否总是将视图加载为肖像? – Sti

+0

和'self.interfaceOrientation'显然是做同样的事情。当从平坦的景观发射,它仍然认为它的肖像.. – Sti

+0

是的,这就是我猜测。根据我的经验,frame/bounds尚未在viewDidLoad中设置。试着像我建议的那样移动它,看看你得到了什么。界面可能还没有旋转(视图控制器总是以纵向启动,然后旋转到横向)。 – lnafziger

1

我挣扎有一点这里是我发现的一些事实:

1-当设备面朝上或向下时,设备在面朝上或向下之前恢复到最后一个方向,因为这两个方向不告诉哟你必须自己决定该设备是纵向还是横向。例如,如果您处于风景状态,然后将设备放平,请启动应用程序,并以横向方向启动。

2-当viewDidLoad被调用时,边界没有被设置,所以你需要在viewWillAppear或viewDidLayoutSubviews中放置任何与方向有关的调用。如果出于某种奇怪的原因,您需要在viewDidLoad之前使用边界,或者可能在模型中做某些事情,我发现放置与方向有关的设置的最佳方式是信任状态栏例如,您可以按照以下方式呼叫。

if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) 

PS:关于你的问题补充,请参阅: https://developer.apple.com/library/ios/#qa/qa2010/qa1688.html

你最有可能最后2发子弹的一个。我就遇到了这个问题之前,很坦率地说找到了最简单的只需要实现:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; 

为所有VC只是为了安全起见,特别是该行为已经从iOS5的改变成iOS6的。

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html#//apple_ref/doc/uid/TP40007457-CH7-SW1

希望这有助于

+0

嗯,现在我开始认为我的设备出了问题设备或我的代码..这行代码不起作用所有..它总是说这是在这里的肖像.. – Sti

+0

回读你的问题,我还没有在模拟器模式(关于你的iPad评论)测试我的评论。另一方面,当你说这行代码不工作,你的意思是在viewDidLoad或ViewDidAppear?还要确保在这些方法中调用super。 – Spectravideo328

+0

我编辑了我的答案,为您的新增加的问题。 – Spectravideo328

相关问题