对于手动添加到层次结构的视图旋转至横向 (iPhone)

发布于 2024-08-15 03:56:09 字数 811 浏览 2 评论 0原文

我有一个简单的 UIViewController,它使用 XIB 作为其界面。

因此,界面简单地包括UIView、UIActivityIndi​​cator和UILabel。我在 Interface Builder 中设置了大小限制,以在视图旋转时保持活动指示器和标签居中。

UIViewController 在 -shouldAutorotateToInterfaceOrientation: 方法中设置为针对纵向和横向返回 YES。

使用“我遇到的问题”手动将视图添加到视图层次结构中,

if (!activityOverlay)
    activityOverlay = [[XBActivityOverlayViewController alloc] initWithNibName:@"XBActivityOverlayViewController" bundle:nil message:@"Connecting..."];

[activityOverlay.view setAlpha:0.0f];
[self.window addSubview:activityOverlay.view];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[activityOverlay.view setAlpha:0.9f];
[UIView commitAnimations];

如果设备已经处于横向方向并且此时将视图添加到层​​次结构中,则视图仍处于纵向方向,并且不会“自动旋转。

我需要做什么才能以与父视图相同的方向添加视图?

I have a simple UIViewController that uses a XIB for its interface.

As such, the interface simply comprises a UIView, a UIActivityIndicator and a UILabel. I have the sizing constraints in Interface Builder set to keep the activity indicator and the label centred when the view rotates.

The UIViewController is set to return YES for portrait and landscape orientations in the -shouldAutorotateToInterfaceOrientation: method.

The view is added to the view hierarchy manually, using

if (!activityOverlay)
    activityOverlay = [[XBActivityOverlayViewController alloc] initWithNibName:@"XBActivityOverlayViewController" bundle:nil message:@"Connecting..."];

[activityOverlay.view setAlpha:0.0f];
[self.window addSubview:activityOverlay.view];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[activityOverlay.view setAlpha:0.9f];
[UIView commitAnimations];

The problem I'm having is that if the device is already in landscape orientation and the view is added to the hierarchy at this point, the view is still in portrait orientation, and doesn't auto rotate.

What do I need to do to add the view in the same orientation as the parent view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

故笙诉离歌 2024-08-22 03:56:09

我不知道这是否是您问题的正确答案,但我希望能有所帮助:

对我来说,每次添加/关闭模式或显示新视图时,都会调用以下函数:

-(void) detectOrientation 
{


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

             item1.frame = CGRectMake(147.0, 241.0, 56.0, 28.0);
     item2.frame = CGRectMake(265.0, 241.0, 56.0, 28.0);

 }
 else if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) ||
          ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) || !inLandscapeOrientation)
 {

    item1.frame = CGRectMake(35.0, 425.0, 35.0, 28.0);
item2.frame = CGRectMake(176.0, 425.0, 35.0, 28.0);
 }

}

这里我根据当前设备方向。也许如果您检测到当前方向是横向,您可以添加具有此类方向的子视图。

亚历杭德拉:)

I do not know if it is the right answer to your question but I hope can help:

For me, every time I add/dismiss a modal or display a new view, the following function is called:

-(void) detectOrientation 
{


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

             item1.frame = CGRectMake(147.0, 241.0, 56.0, 28.0);
     item2.frame = CGRectMake(265.0, 241.0, 56.0, 28.0);

 }
 else if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) ||
          ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) || !inLandscapeOrientation)
 {

    item1.frame = CGRectMake(35.0, 425.0, 35.0, 28.0);
item2.frame = CGRectMake(176.0, 425.0, 35.0, 28.0);
 }

}

Here I change the position according to the current device orientation. Maybe if you detect that the current orientation is landscape you add a subview that has such orientation.

Alejandra :)

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