如何通过选项卡和视图来检测设备方向?

发布于 2024-08-28 00:33:26 字数 1153 浏览 8 评论 0原文

我的应用程序在纵向和横向之间来回切换。我已将所有内容(标签、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-09-04 00:33:26

在每个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文