菜鸟教程小白 发表于 2022-12-12 12:05:55

ios - iOS6下多个xib旋转的问题


                                            <p><p>我需要为纵向和横向使用不同的 xib 文件。我没有使用自动布局,但我使用的是 iOS6。 (如果您关心原因,请参阅 <a href="https://stackoverflow.com/questions/17432321/can-ioss-auto-layout-rearrange-ui-widgets" rel="noreferrer noopener nofollow">my previous question</a>。)</p>

<p>我正在关注亚当对 <a href="https://stackoverflow.com/questions/2496554/easiest-way-to-support-multiple-orientations-how-do-i-load-a-custom-nib-when-th/" rel="noreferrer noopener nofollow">this question</a> 的回答用 amergin 的 initWithNib 名字技巧修改,用我自己的 iPhone/iPad 需要修改。这是我的代码:</p>

<pre><code>-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{   
    [ loadNibNamed:
                                  owner: self
                              options: nil];
    ;
}

- (NSString *) xibNameForDeviceAndRotation:(UIInterfaceOrientation)toInterfaceOrientation
{
    NSString *xibName ;
    NSString *deviceName ;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
      deviceName = @&#34;iPad&#34;;
    } else {
      deviceName = @&#34;iPhone&#34;;
    }

    if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) )
    {
      xibName = )];
      if([ pathForResource:xibName ofType:@&#34;nib&#34;] != nil) {
            return xibName;
      } else {
            xibName = ), deviceName];
            if([ pathForResource:xibName ofType:@&#34;nib&#34;] != nil) {
                return xibName;
            } else {
                NSAssert(FALSE, @&#34;Missing xib&#34;);
                return nil;
            }
      }

    } else {
      xibName = )];
      if([ pathForResource:xibName ofType:@&#34;nib&#34;] != nil) {
            return xibName;
      } else {
            xibName = ), deviceName];
            if([ pathForResource:xibName ofType:@&#34;nib&#34;] != nil) {
                return xibName;
            } else {
                NSAssert(FALSE, @&#34;Missing xib&#34;);
                return nil;
            }
      }
    }
}
</code></pre>

<p>当然我在做:</p>

<pre><code>- (BOOL) shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}
</code></pre>

<p>在我的 ViewController 中:</p>

<pre><code>- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}
</code></pre>

<p>在我的委托(delegate)中。</p>

<p>我有两个可能相关的问题。第一,简单的。我不倒转。我为 iPad 和 iPhone 在 xcode 中打开了所有正确的位。这可能是一个单独的问题,也可能是我的问题的核心。</p>

<p>真正的问题是,当我旋转到横向模式时,我的 xib 被替换,但 View 偏离了 90 度。</p>

<p>这是我的 2 xib 的样子。 (我给它们涂上了花哨的颜色,所以你可以看到它们是不同的。)</p>

<p> <img src="/image/cnPV6.jpg" alt="enter image description here"/>和 <img src="/image/GNeMf.jpg" alt="enter image description here"/> </p>

<p>当我运行它时(最初在横向模式下),您可以看到横向 xib 是正确的。</p>

<p> <img src="/image/RTBO0.jpg" alt="enter image description here"/> </p>

<p>当我旋转到纵向时它也是正确的</p>

<p> <img src="/image/SgmUW.jpg" alt="enter image description here"/> </p>

<p>但是当我旋转回横向时,xib 被替换,但 View 偏离了 90 度。</p>

<p> <img src="/image/Lk0Yp.jpg" alt="enter image description here"/> </p>

<p>这里有什么问题?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我可能一直在走与保罗·塞尚去年相同的道路。不确定他是否尝试过这个,但我解决了最初的问题(在这个问题中说明),只是让我的根 Controller 成为导航 Controller 而不是我的 ViewController 类。由于我使用的是“空项目”模板和 XIB 文件,这意味着更改正常:</p>

<pre><code>self.viewController = [ initWithNibName:@&#34;ViewController&#34; bundle:nil];
self.window.rootViewController = self.viewController;
</code></pre>

<p>在 AppDelegate.m 中,改为:</p>

<pre><code>self.viewController = [ initWithNibName:@&#34;ViewController&#34; bundle:nil];
UINavigationController *navigationController = [ initWithRootViewController:self.viewController];
navigationController.navigationBar.hidden = YES;
self.window.rootViewController = navigationController;
</code></pre>

<p>也就是说,我刚刚创建了一个通用 UINavigationController 并将其设置为 Root ViewController 。</p>

<p>我不确定这是否会导致其他问题,并且可能有一种方法可以弄清楚(也许您需要源代码)UINavigationController 做了哪些 UIViewController 没有。可以像在正确的地方调用一个额外的 setNeedsLayout 类型一样简单。如果我弄明白了,我会为 future 的读者编辑这个答案。</p>

<p>归功于 Sakti 对 <a href="https://stackoverflow.com/questions/2496554/easiest-way-to-support-multiple-orientations-how-do-i-load-a-custom-nib-when-th/" rel="noreferrer noopener nofollow">Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?</a> 的评论我第一次阅读它们时不应该忽略它们:</p>

<blockquote>
<p>i added the view controller to navigation controller and presented it
which made it work as intended</p>
</blockquote>

<p>编辑:在示例代码中添加了额外的行以隐藏导航栏,因为大多数关注此问题的人都不希望这样做。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iOS6下多个xib旋转的问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17435397/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17435397/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iOS6下多个xib旋转的问题