我有一个包含在导航 Controller 中的 TableView Controller 。当通过 presentViewController:animated:completion:
呈现时,导航 Controller 似乎会自动将正确的内容插入 TableView Controller 。 (谁能给我解释一下这是如何工作的?)
但是,一旦我将组合包装在自定义容器 View Controller 中并显示它, TableView 内容的最顶部部分就会隐藏在导航栏后面。为了在此配置中保留自动内容插入行为,我能做些什么吗?我是否必须“通过”容器 View Controller 中的某些内容才能正常工作?
我想避免手动或通过自动布局调整内容插入,因为我想继续支持 iOS 5。
最佳答案
我遇到过类似的问题。所以在 iOS 7 上,UIViewController
上有一个名为 automaticallyAdjustsScrollViewInsets
的新属性.默认情况下,它设置为 YES
。当您的 View 层次结构比导航或标签栏 Controller 中的滚动/表格 View 更复杂时,此属性似乎对我不起作用。
我所做的是:我创建了一个基本 View Controller 类,所有其他 View Controller 都继承自该类。然后该 View Controller 负责显式设置插图:
标题:
#import <UIKit/UIKit.h>
@interface SPWKBaseCollectionViewController : UICollectionViewController
@end
实现:
#import "SPWKBaseCollectionViewController.h"
@implementation SPWKBaseCollectionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateContentInsetsForInterfaceOrientation:self.interfaceOrientation];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self updateContentInsetsForInterfaceOrientation:toInterfaceOrientation];
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)updateContentInsetsForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
UIEdgeInsets insets;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
insets = UIEdgeInsetsMake(64, 0, 56, 0);
} else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (UIInterfaceOrientationIsPortrait(orientation)) {
insets = UIEdgeInsetsMake(64, 0, 49, 0);
} else {
insets = UIEdgeInsetsMake(52, 0, 49, 0);
}
}
self.collectionView.contentInset = insets;
self.collectionView.scrollIndicatorInsets = insets;
}
}
@end
这也适用于 WebView :
self.webView.scrollView.contentInset = insets;
self.webView.scrollView.scrollIndicatorInsets = insets;
如果有更优雅但更可靠的方法来做到这一点,请告诉我!硬编码的插入值闻起来很糟糕,但当你想保持 iOS 5 兼容性时,我真的没有看到其他方法。
关于iOS 7 : Custom container view controller and content inset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19065157/