如何通过选项卡和视图来检测设备方向?
我的应用程序在纵向和横向之间来回切换。我已将所有内容(标签、uiimageviews、uilabels)排列整齐,以便在两个方向上重新定位。然而,只有当设备实际旋转时才会发生变化。当我在旋转标签后在选项卡之间循环时,它只会显示自动调整大小,而不是用户旋转它时我设置的方式。
如何检测设备的方向并在点击不同选项卡时显示视图以反映方向?
好的,我将其设置为 viewWillAppear。它没有检测方向并显示我设置的内容是否有原因?
-(void)viewWillAppear:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
[UIView beginAnimations:@"move buttons" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if(toOrientation == UIInterfaceOrientationPortrait
|| toOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
aboutContent.frame = CGRectMake(20, 100, 280, 107);
myLogo.frame = CGRectMake(-5, 20, 330, 65);
bulletOne.frame = CGRectMake(40, 220, 240, 45);
bulletTwo.frame = CGRectMake(40, 270, 240, 45);
}
else
{
aboutContent.frame = CGRectMake(40, 80, 400, 70);
myLogo.frame = CGRectMake(230, 30, 20, 20);
bulletOne.frame = CGRectMake(90, 140, 330, 65);
bulletTwo.frame = CGRectMake(90, 170, 330, 65);
}
[UIView commitAnimations];
}
My application goes back and forth between Portrait and Landscape. I have all of my content(labels, uiimageviews, uilabels) lined up to relocate for both orientations. However, the only change when the device is actually rotated. When I cycle between tabs after it has been rotated it just shows up autosized and not the way I have it setup when the user rotates it.
How can I detect the orientation of the device and display the view to reflect the orientation when the taps different tabs?
Ok, I set it to viewWillAppear. Is there a reason why it isn't detecting the orientation and displaying the content where I have set it?
-(void)viewWillAppear:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
[UIView beginAnimations:@"move buttons" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if(toOrientation == UIInterfaceOrientationPortrait
|| toOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
aboutContent.frame = CGRectMake(20, 100, 280, 107);
myLogo.frame = CGRectMake(-5, 20, 330, 65);
bulletOne.frame = CGRectMake(40, 220, 240, 45);
bulletTwo.frame = CGRectMake(40, 270, 240, 45);
}
else
{
aboutContent.frame = CGRectMake(40, 80, 400, 70);
myLogo.frame = CGRectMake(230, 30, 20, 20);
bulletOne.frame = CGRectMake(90, 140, 330, 65);
bulletTwo.frame = CGRectMake(90, 170, 330, 65);
}
[UIView commitAnimations];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在每个 UIViewController 的 viewWillAppear 中检查设备的方向,如果方向与您的布局方式不同,则发送 willRotateToInterfaceOrientation:duration: 到 self。假设您的 willRotateToInterfaceOrientation:duration: 会将布局更新为指定的方向。
In each UIViewController's viewWillAppear check the device's orientation and if the orientation is different from how you have it layed out then send willRotateToInterfaceOrientation:duration: to self. That's assuming that your willRotateToInterfaceOrientation:duration: will update the layout to the specified orientation.