在我的应用程序中,我想根据 iPhone 的主页按钮方向设置 UIView 方向。我使用以下方式完成了它:
/* 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。
但是如果我设置 return Yes 而不是 return (interfaceOrientation == UIInterfaceOrientationPortrait);那么它就不起作用了。
如何解决这个问题。如果有人知道的话,请帮助我。 LifeCards 应用程序中也实现了类似的功能。 提前致谢。
最佳答案
上面的方法是当设备方向改变时决定是否将 View 旋转到特定方向。
return (interfaceOrientation == UIInterfaceOrientationPortrait);
指定仅支持纵向。
return YES;
支持所有方向。
但是,如果您将 View Controller 放入 TabBarController 中,那么仅当所有 View Controller 都支持该特定方向时,它才会旋转。
不要将上面的代码放在 willRotateToInterfaceOrientation 中自动调整对象的大小。
但是你需要知道一件事,通过指定
UIViewAutoresizingFlexibleLeftMargin
您只是指定 View 可以在对象和左边距之间放置尽可能多的空间。但在方向更改期间,它会保留其他侧的先前位置,因此您可能需要在物理上更改对象的原点(viewForBarButtons)
好的。我猜你的意思是你想要 viewForBarButtons 位于 homeButton 旁边,并且需要相应地定位/旋转其 subview 。
首先注册 devie Rotaion 或使用 didRotateInterfaceOrientation 来启动 viewForBarButtons 的旋转 subview 。
#define degreesToRadians(x) (M_PI * x / 180.0)
旋转 subview :用 subview 对象替换 self
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];
关于iphone - 修复 uiview 在 iphone sdk 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312849/