【问题标题】:Rotating Not Detected未检测到旋转
【发布时间】:2012-06-12 15:29:41
【问题描述】:

我正在使用:

 if (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight)
{
    viewofimage.frame = CGRectMake(130, 45, 220, 115);
    share.frame = CGRectMake(205, 161, 70, 70);
    invite.frame = CGRectMake(8, 161, 70, 70);
    contact.frame = CGRectMake(402, 161, 70, 70);
    invitation.frame = CGRectMake(3, 227, 81, 21);
    sharing.frame = CGRectMake(200, 227, 81, 21);
    contacting.frame = CGRectMake(397, 227, 81, 21);
}
else
{
    viewofimage.frame = CGRectMake(20, 64, 280, 206);
    invite.frame = CGRectMake(8, 285, 70, 70);
    share.frame = CGRectMake(125, 285, 70, 70);
    contact.frame = CGRectMake(242, 285, 70, 70);
    invitation.frame = CGRectMake(3, 358, 81, 21);
    sharing.frame = CGRectMake(120, 358, 81, 21);
    contacting.frame = CGRectMake(237, 358, 81, 21);
}

旋转时在特定位置设置按钮和标签。唯一的问题是,当我离开该视图控制器并转到另一个控制器时,使用相同的代码,它不会将按钮和标签移动到定义的 CGRECTMAKE 值。它仅在选定的视图控制器旋转时移动它们。如何让其他视图控制器检测它所处的方向,并在到达它们时正确调整其大小?

【问题讨论】:

    标签: iphone xcode resize orientation


    【解决方案1】:

    设置 UIView 的 autoMaskSubView 为 false 使用这种方法:

     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    
    if (interfaceOrientation = UIInterfaceOrientationLandscapeLeft || interfaceOrientation = UIInterfaceOrientationLandscapeRight)
    {
    viewofimage.frame = CGRectMake(130, 45, 220, 115);
    share.frame = CGRectMake(205, 161, 70, 70);
    invite.frame = CGRectMake(8, 161, 70, 70);
    contact.frame = CGRectMake(402, 161, 70, 70);
    invitation.frame = CGRectMake(3, 227, 81, 21);
    sharing.frame = CGRectMake(200, 227, 81, 21);
    contacting.frame = CGRectMake(397, 227, 81, 21);
    }
    else
    {
    viewofimage.frame = CGRectMake(20, 64, 280, 206);
    invite.frame = CGRectMake(8, 285, 70, 70);
    share.frame = CGRectMake(125, 285, 70, 70);
    contact.frame = CGRectMake(242, 285, 70, 70);
    invitation.frame = CGRectMake(3, 358, 81, 21);
    sharing.frame = CGRectMake(120, 358, 81, 21);
    contacting.frame = CGRectMake(237, 358, 81, 21);
    }
    
    return YES; 
    }
    

    【讨论】:

    • 这是有效的,但我遇到了一个问题。每个选项卡都是一个导航控制器,我将导航栏设置为隐藏。一些按钮推动了一个带有自己的 xib 的表格视图,需要有导航栏,所以当这些按钮出现时,我取消隐藏导航栏。这会导致 UIButtons 向下滑动导航栏的高度,而不是 ImageView。关于如何防止这种情况发生的任何想法?
    【解决方案2】:

    View Controller 每次检测到旋转时都会调用此方法。我认为这就是您目前拥有此代码的地方

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
            viewofimage.frame = CGRectMake(130, 45, 220, 115);
            share.frame = CGRectMake(205, 161, 70, 70);
            invite.frame = CGRectMake(8, 161, 70, 70);
            contact.frame = CGRectMake(402, 161, 70, 70);
            invitation.frame = CGRectMake(3, 227, 81, 21);
            sharing.frame = CGRectMake(200, 227, 81, 21);
            contacting.frame = CGRectMake(397, 227, 81, 21);
        }
        else
        {
            viewofimage.frame = CGRectMake(20, 64, 280, 206);
            invite.frame = CGRectMake(8, 285, 70, 70);
            share.frame = CGRectMake(125, 285, 70, 70);
            contact.frame = CGRectMake(242, 285, 70, 70);
            invitation.frame = CGRectMake(3, 358, 81, 21);
            sharing.frame = CGRectMake(120, 358, 81, 21);
            contacting.frame = CGRectMake(237, 358, 81, 21);
        }
    }
    

    -=-=-=-=-=-=-=-=-

    如果您想在移动到不同的 viewController 时检测方向并相应地放置对象...那么您可以像这样检测您所处的方向:

    -(void)viewWillAppear:(BOOL)animated
    {
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
        {
            viewofimage.frame = CGRectMake(130, 45, 220, 115);
            share.frame = CGRectMake(205, 161, 70, 70);
            invite.frame = CGRectMake(8, 161, 70, 70);
            contact.frame = CGRectMake(402, 161, 70, 70);
            invitation.frame = CGRectMake(3, 227, 81, 21);
            sharing.frame = CGRectMake(200, 227, 81, 21);
            contacting.frame = CGRectMake(397, 227, 81, 21);
        }
        else
        {
            viewofimage.frame = CGRectMake(20, 64, 280, 206);
            invite.frame = CGRectMake(8, 285, 70, 70);
            share.frame = CGRectMake(125, 285, 70, 70);
            contact.frame = CGRectMake(242, 285, 70, 70);
            invitation.frame = CGRectMake(3, 358, 81, 21);
            sharing.frame = CGRectMake(120, 358, 81, 21);
            contacting.frame = CGRectMake(237, 358, 81, 21);
        }
    }
    

    -=-=-=-=-=-=-=-=-

    进一步参考:

    How to programmatically determine iPhone interface orientation?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2011-03-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      相关资源
      最近更新 更多