iOS7中的UIView真实帧

时间:2013-09-17 21:58:50

标签: ios6 uiview uiviewcontroller ios7

我需要在iOS 7中添加一些视图。与此同时,我的项目必须在iOS 6上运行。

enter image description here

我怎样才能编程呢?我希望在2个版本的app上看到“i”按钮的相同位置。

4 个答案:

答案 0 :(得分:3)

这是我放在ViewController上的一个类别。它可能不是最聪明的方法,但它适用于我的观点。我的所有应用程序都以编程方式构建UI。如果在加载视图后将其设置为此视图,则iOS 7中的行为与早期版本相同。代码不是kludgy,因为它不依赖于版本而非superview的特征。我愿意接受任何评论。

@implementation UIViewController (effectiveContentFrame)

// determine frame size and offset to get the max frame we can get for the current view and orientation
// needed this after the introduction of iOS 7 where frames are handled slightly differently
// note: it uses the different heights/widths at the current rotation


/*
 iOS 6 portrait
  application Frame: = (0.000000,20.000000) 320.000000x460.000000 )
  superview Frame = (0.000000,20.000000) 320.000000x460.000000 )

 iOS7 portrait
  application Frame: = (0.000000,20.000000) 320.000000x460.000000 )
  superview Frame = (0.000000,0.000000) 320.000000x480.000000 )

 */
- (CGRect) effectiveContentFrame
    {
    CGRect rect;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
    CGFloat statusBarHeight = 0.;

    rect = self.view.superview.frame; // the frame will be relative to the superview. Note in iOS6 the root superview frame includes the

    if (UIInterfaceOrientationIsPortrait(orientation))
        {
        rect.size.height += rect.origin.y; // total height
        statusBarHeight = statusBarFrame.size.height;
        }
    else
        {
        rect.size.height += rect.origin.x; // total height
        statusBarHeight = statusBarFrame.size.width;
        }

    CGFloat extraHeight = 0;    // this is going to be how much extra height we have to take off of the total height of the frame,
                                // which is going to be the status bar+navbar+toolbar heights
    CGFloat topHeight = 0;      // this is the height of the stuff before the frame, which we are going to use as an offset


    if (![UIApplication sharedApplication].statusBarHidden)
        extraHeight += statusBarHeight;
   if (!self.navigationController.navigationBar.isHidden)
        extraHeight += self.navigationController.navigationBar.frame.size.height;
    topHeight = extraHeight;
    if (!self.navigationController.toolbar.isHidden)
        extraHeight += self.navigationController.toolbar.frame.size.height;

    rect.origin.y = topHeight - rect.origin.y;  // in iOS6 the status bar and navbar is already included in the origin but not in iOS7 .
    if (rect.origin.y < 0)
        rect.origin.y = 0;


    rect.size.height -= extraHeight;    // subtract status/nav and toolbar heights from the total height left in the superview

    return rect;
}

答案 1 :(得分:3)

您的主视图顶部栏的类型似乎是半透明导航栏。尝试将其在IB中更改为不透明导航栏。

升级到新的xCode后,我遇到了同样的问题。我认为这是因为在旧版本中,opaque类型是列表中的第一个,现在是半透明的。

此外,您可以在UIViewController中尝试self.edgesForExtendedLayout = UIRectEdgeNone;

答案 2 :(得分:0)

在Xcode 51B中,您可以找到iOS 6/7增量。您需要的只是调整它们。 Example

答案 3 :(得分:0)

C#中的答案

    public static RectangleF GetUsableContentSize(this UIViewController viewController) {
        RectangleF rect;
        UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
        RectangleF statusBarFrame = UIApplication.SharedApplication.StatusBarFrame;
        float statusBarHeight = 0.0f;

        var view = viewController.View;

        if (view.Superview != null)
            rect = view.Superview.Frame; // the frame will be relative to the superview. Note in iOS6 the root superview frame includes the
        else
            rect = view.Frame;


        if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown)
        {
            rect.Height += rect.Y; // total height
            statusBarHeight = statusBarFrame.Height;
        }
        else
        {
            rect.Height += rect.X; // total height
            statusBarHeight = statusBarFrame.Width;
        }

        float extraHeight = 0;    // this is going to be how much extra height we have to take off of the total height of the frame,
        // which is going to be the status bar+navbar+toolbar heights
        float topHeight = 0;      // this is the height of the stuff before the frame, which we are going to use as an offset


        if (!UIApplication.SharedApplication.StatusBarHidden)
            extraHeight += statusBarHeight;

        if (!viewController.NavigationController.NavigationBar.Hidden)
            extraHeight += viewController.NavigationController.NavigationBar.Frame.Height;

        topHeight = extraHeight;
        if (!viewController.NavigationController.Toolbar.Hidden)
            extraHeight += viewController.NavigationController.Toolbar.Frame.Height;

        rect.Y = topHeight - rect.Y;  // in iOS6 the status bar and navbar is already included in the origin but not in iOS7 .
        if (rect.Y < 0)
            rect.Y = 0;


        rect.Size.Height -= extraHeight;    // subtract status/nav and toolbar heights from the total height left in the superview

        return rect;
    }