2012-04-25 54 views
0

在我的应用程序中,我想根据iPhone的home按钮方向设置UIView方向。我使用以下方法完成:修复iphone中的uiview位置sdk

/* I handled view orientation in shouldAutorotate method*/ 
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
      viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
      viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 
     else if (interfaceOrientation == UIInterfaceOrientationPortrait) 
      viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin; 
     else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
      viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 

其中viewForBarButtons是viewController中的UIView。

但是,如果我设置返回是而不是返回(interfaceOrientation == UIInterfaceOrientationPortrait);那么它不起作用。

如何解决此问题。如果有人知道它,那么请帮助我。 类似的功能在LifeCards应用中实现。 在此先感谢。

回答

1

以上方法是决定是否将视图旋转到特定的方向,当设备方向改变时。

return (interfaceOrientation == UIInterfaceOrientationPortrait); 

指定仅支持纵向定位。

return YES; 

将支持所有的方向。

但是,如果您将您的viewcontroller放在TabBarController中,那么只有在所有viewcontroller都支持该特定方向时,它才会旋转。

而不是把上面的代码自动调整对象在willRotateToInterfaceOrientation。

但你需要知道一两件事,通过specifing

UIViewAutoresizingFlexibleLeftMargin

你只是指定视图可以把尽可能多的空间要对象和左边空白之间。但是它会在方向改变时保持以前的位置,所以你可能需要在物理上改变对象的原点(viewForBarButtons)

好的。我想你在说什么是你想在homeButton旁边的viewForBarButtons,并需要相应地定位/旋转它的子视图。

devie rotaion的第一个注册表或使用didRotateInterfaceOrientation来启动viewForBarButtons的旋转子视图。

#define degreesToRadians(x) (M_PI * x/180.0) 

旋转子视图:与子视图对象

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 

    if (animated) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 
    } 

    if (orientation == UIInterfaceOrientationPortraitUpsideDown) 
     self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(180)); 

    else if (orientation == UIInterfaceOrientationLandscapeRight) 
     self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(90)); 

    else if (orientation == UIInterfaceOrientationLandscapeLeft) 
     self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(-90)); 
    else 
     self.transform=CGAffineTransformIdentity; 

    if (animated) 
     [UIView commitAnimations]; 
+0

替代自行但是,如果我改变观点的起源,然后它的子视图得到打扰。 – Girish 2012-04-25 10:57:09

+0

可能有一种可能性。创建一个大小为父视图(只有填充需要比viewForBarButtons更多)并添加viewForBarButtons到它。为viewForBarButton设置所需的自动调整大小掩码。添加superview到contatiner并调整到整个视图(不要忘记设置灵活的高度和宽度)。这应该工作。 – 2012-04-25 11:24:58

+0

我曾评论说,这将是“在改变方向期间在其他方面的先前位置”。它会根据基于它所添加的超级观点的立场来审议案件。对先前的错误抱歉。 – 2012-04-25 11:26:27