iOS 7 : Custom container view controller and content inset

标签 ios objective-c uiviewcontroller ios7

我有一个包含在导航 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/

相关文章:

ios - UITabBar 自动旋转问题

uiviewcontroller - 切换到另一个 View ,但保留在导航 Controller 中

ios - 即使在弹出 View Controller 之后,CLLocationManager 委托(delegate)仍然被调用

ios - 如何在 iOS 中创建类似于 Windows 窗体的选项卡控件?

objective-c - 如何检查对象是否已初始化? Objective-C

ios - 如何检查 View Controller 是否以模态方式呈现或推送到导航堆栈上?

android - 如何检查应用程序是否已从 android/Iphone 设备上卸载

ios - Swift iOS - 如何将 Firebase token 发送到 Heroku 上的 Https URL?

ios - NSPredicate 针对 NSArray 中的嵌套值

ios - 从父 View Controller 访问容器 View 中的 UIScrollView 内容偏移量