设置“edgesForExtendedLayout = UIRectEdgeNone”不会阻止我的子视图在 iOS 7 中向上移动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20689792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:49:39  来源:igfitidea点击:

Setting "edgesForExtendedLayout = UIRectEdgeNone" does not prevent my subview from moving up in iOS 7

iosiphoneobjective-cios7

提问by BeachRunnerFred

I have a view controller that was written back in the days of iOS 5 and I'm trying to transition it to iOS 7. After reading the iOS 7 transition guide and poking around on SO, I discovered that I need to set the new iOS 7 property edgesForExtendedLayoutto UIRectEdgeNoneto prevent one of my custom subviews from appearing 49 pixels higher on iOS 7 than it appears on iOS 6. However, after setting that property, my custom subview is still appearing 49 pixels higher on iOS 7 and I don't know what else I need to do. Here's my simple code that I added to my viewDidLoadmethod...

我有一个在 iOS 5 时代写回来的视图控制器,我正在尝试将它转换到 iOS 7。在阅读了 iOS 7 转换指南并在 SO 上闲逛后,我发现我需要设置新的 iOS 7 属性edgesForExtendedLayoutUIRectEdgeNone以防止我的自定义子视图之一在 iOS 7 上显示的像素比在 iOS 6 上高 49 像素。但是,设置该属性后,我的自定义子视图在 iOS 7 上仍然显示高 49 像素,我不知道我还需要做什么。这是我添加到我的viewDidLoad方法中的简单代码...

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

and here's the code for creating and adding the custom subview that is appearing higher on iOS 7...

这是用于创建和添加在 iOS 7 上显示更高的自定义子视图的代码......

CGRect customControlFrameRect = {{0.0f, 240.0f}, {100.0f, 100.0f}};
self.customControl = [[MyCustomControl alloc] initWithFrame:customControlFrameRect];

self.customControl.autoresizingMask =  UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:self.customControl];

One other important detail, if it helps, is this view is created from a nib file, but the custom subview that is appearing higher on iOS 7 than iOS 6 is the only subview that is created and added programmatically in viewDidLoad, after I set the edgesForExtendedLayoutproperty. All the other subviews that are created from the nib aren't affected regardless of the whether or not I set the egdesForExtendedLayoutproperty.

另一个重要的细节,如果有帮助的话,是这个视图是从一个 nib 文件创建的,但是在 iOS 7 上出现比 iOS 6 更高的自定义子视图是viewDidLoad在我设置edgesForExtendedLayout属性后以编程方式创建和添加的唯一子视图. 无论我是否设置了该egdesForExtendedLayout属性,从笔尖创建的所有其他子视图都不会受到影响。

My two questions are...

我的两个问题是...

  1. Why is my custom subview appearing higher on iOS 7 even after I set the edgesForExtendedLayout property to UIRectEdgeNone?
  2. Why aren't the other subviews (the subviews that are loaded from the nib) appearing higher in iOS 7?
  1. 为什么即使在我将edgesForExtendedLayout 属性设置为UIRectEdgeNone 之后,我的自定义子视图在iOS 7 上仍然显示得更高?
  2. 为什么其他子视图(从笔尖加载的子视图)在 iOS 7 中没有出现在更高的位置?

Thanks in advance for your wisdom!

在此先感谢您的智慧!

回答by Binarian

1.

1.

It is the purpose of edgesForExtendedLayoutwhich was set to UIRectEdgeNoneto notextend the view in any direction, but your view will now start in origin within the screen size (now below the statusBar!) when using a frame with position of 0, 0 (like you did with the customControl). You need to adjust the frame of your view/viewController to represent that you want to start under the statusBar/navigationBar or make the status/nagivationBar opaque; or add UIRectEdgeTop if only used for one view.

它的目的,edgesForExtendedLayout它被设置为UIRectEdgeNone使用带,0 0位置的帧时在任何方向上延伸的观点,但你的观点现在在屏幕尺寸中的原点开始(现在下面状态栏!)(喜欢你用customControl)做的。您需要调整您的view/viewController 的frame 来表示您要在statusBar/navigationBar 下启动或使status/nagivationBar 不透明;如果仅用于一个视图,则添加 UIRectEdgeTop。

Setting the navigationController to opaque will also do so with the statusBar:

将 navigationController 设置为 opaque 也将使用 statusBar 这样做:

self.navigationController.navigationBar.translucent = NO;

2.

2.

Because the interface buildersets the framecorrectly under the navigationBar by default, you could change it of course. And it uses UIRectEdgeAlland autoresizing.

因为默认情况下界面构建器会在导航栏下正确设置框架,所以您当然可以更改它。它使用UIRectEdgeAll和自动调整大小

回答by Mike

I've found, in some cases, that setting the edgesForExtendedLayoutin viewWillAppear:rather than viewDidLoad gives me the result I'm looking for. This may not solve yourproblem, but it has helped me in a few cases and hopefully it'll help others.

我发现,在某些情况下,设置edgesForExtendedLayoutinviewWillAppear:而不是 viewDidLoad 会给我我正在寻找的结果。这可能无法解决您的问题,但它在一些情况下帮助了我,希望它会帮助其他人。

回答by adali

   if( [self respondsToSelector:@selector(setEdgesForExtendedLayout:)] )
    {
        self.edgesForExtendedLayout=UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars=NO;
        self.automaticallyAdjustsScrollViewInsets=NO;
    }