gpt4 book ai didi

iphone - 重新创建 UIViewcontroller 的 View 时,如何强制旋转?

转载 作者:行者123 更新时间:2023-11-28 20:27:46 28 4
gpt4 key购买 nike

我有一个正在重复使用的 View Controller (出于内存限制原因。)因此,我没有推送新的 UIViewController,而是设置了一些参数,然后强制此 VC 重新加载其 View 。代码是这样的(由通知​​回调触发):

- (void) reloadView: (NSNotification*) note
{
// Save parameters for reloaded view
UIWindow *window = (UIWindow*) self.view.superview;
//CGAffineTransform xfrm = self.view.transform; // Doesn't do what I want.

// Trash this one & reload the view
[self.view removeFromSuperview];
self.view = nil;

// force view reload
if (self.view == nil)
{
NSLog(@"%s ***** Why didn't self.view reload?!", __PRETTY_FUNCTION__);
}
else
{
// restore params
//self.view.transform = xfrm; // Boo-hoo!
[window addSubview: self.view];
}
}

一切正常除了应用程序只是横向的,并且新重新加载的 View 作为纵向添加到窗口。

我尝试将旧 View 强制转换到新 View 上,但奇怪的是,它提供了旋转但有一个愚蠢的平移偏移。

有没有办法告诉 UIViewController“现在开始旋转”......?

编辑:

我添加了这个相当愚蠢的技巧:

    // restore params
self.view.transform = xfrm;
self.view.center = CGPointMake(window.bounds.size.width / 2., window.bounds.size.height / 2.);
[window addSubview: self.view];

这给出了想要的结果,但我真的很不高兴在我的代码库中有这样的东西。当然有更好的方法来做到这一点?!?!

谢谢!

编辑:

在与 JPH 讨论后,答案是“不要那样做”。有关一些细节和发生的重新设计,请参阅评论。

最佳答案

你的问题在于使用这个:

[window addSubview: self.view];

来自文档:

If you add an additional view controller's UIView property to UIWindow (at the same level as your primary view controller) via the following:

[myWindow addSubview:anotherController.view];

this additional view controller will not receive rotation events and will never rotate. Only the first view controller added to UIWindow will rotate.

https://developer.apple.com/library/ios/#qa/qa2010/qa1688.html

我更喜欢带有 Root View Controller 的设计,并且 subview 被添加到 Root View Controller 的 View 中。

另一种选择是不终止 View 并重新添加它,而是更新该 View 中需要更新的所有内容。我不确定我是否理解您为什么想要终止 View 并立即重新添加它。

关于iphone - 重新创建 UIViewcontroller 的 View 时,如何强制旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13409327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com