我需要在 iOS 7 中添加一些视图。同时我的项目必须在 iOS 6 上运行。
我怎样才能以编程方式做到这一点?我想在 2 个版本的应用程序上看到“i”按钮的相同位置。
我需要在 iOS 7 中添加一些视图。同时我的项目必须在 iOS 6 上运行。
我怎样才能以编程方式做到这一点?我想在 2 个版本的应用程序上看到“i”按钮的相同位置。
这是我放在 ViewController 上的一个类别。这可能不是最聪明的方法,但它适用于我的观点。我所有的应用程序都以编程方式构建 UI。如果在加载后将视图的框架设置为此,它将在 iOS 7 中与早期版本中的行为相同。该代码并不笨拙,因为它不依赖于版本,而是依赖于超级视图的特征。我愿意接受任何评论。
@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;
}
您的主视图的顶部栏类型似乎是半透明导航栏。尝试在 IB 中将其更改为不透明导航栏。
当我升级到新的 xCode 时,我遇到了同样的问题。我认为这是因为在旧版本中,不透明类型在列表中排在第一位,现在在它的位置上是半透明的。
此外,您可以尝试self.edgesForExtendedLayout = UIRectEdgeNone;
在 UIViewController
在 Xcode 5 IB 中,您可以找到 iOS 6 / 7 增量。您所需要的只是调整它们。
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;
}