15

我有一个视图控制器,它通过该loadView方法以编程方式创建其视图。视图的目的是显示数据模型的内容。当视图加载时,它在状态和导航栏下正确对齐。

但稍后我会以模态方式呈现另一个视图,以允许用户选择不同的数据模型来填充视图。这会导致loadView再次调用 并为新数据模型重新创建所需的 UI。问题是视图的内容现在出现在状态栏和导航栏下!

请注意,由于必须支持 iOS4,我没有使用自动布局 :( 另外,如果我包含extendedLayout修复程序 - 它确实解决了问题,但只有在模式的关闭动画完成后才会出现跳下效果。我的代码在下面,谢谢任何帮助。

- (void)loadView {

CGRect frame = CGRectMake(0, 0, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];

CGRect scrollFrame = CGRectMake(0, 0, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
        UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];

_toolbarViewController = [self createToolbarViewController];
[self.view addSubview:_toolbarViewController.view];
_toolbarViewController.productInfoWorkflowState = _productInfoWorkflowState;

UIView *containerView = [[UIView alloc] initWithFrame:scrollView.frame];
containerView.backgroundColor = [UIColor clearColor];

_headerViewController = [self createHeaderViewController];
[containerView addSubview:_headerViewController.view];

_menuViewController = [[ProductInfoMenuViewController alloc] initWithBatch:[self batchData]];
_menuViewController.delegate = self;
[containerView addSubview:_menuViewController.view];

CGRect categoriesFrame = _menuViewController.view.frame;
categoriesFrame.origin.y = _headerViewController.view.frame.size.height;
_menuViewController.view.frame = categoriesFrame;

CGRect viewFrame = containerView.frame;
viewFrame.size.height = _headerViewController.view.frame.size.height + _menuViewController.view.frame.size.height;
containerView.frame = viewFrame;

[scrollView addSubview:containerView];
scrollView.contentSize = containerView.frame.size;

_starViewController = [[StarViewController alloc] initForProduct:_productData With:[StarredItems new]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_starViewController.view];
}

首次加载后的屏幕布局:

首次加载后的屏幕布局

第二次加载后的屏幕布局:

第二次加载后的屏幕布局

通过 Leo 的建议修复,scrollView 是正确的,但底部的工具栏现在显示不正确。我使用的代码(放在上面的工具栏创建代码之后):

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    self.automaticallyAdjustsScrollViewInsets = NO;
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
    scrollView.contentInset = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
}

结果:

在此处输入图像描述

4

6 回答 6

14

添加以下代码对我来说同样的问题,可能对你有帮助

/* ios 7 Change */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
于 2013-10-18T13:26:46.293 回答
3

这只发生在 iOS 7 上吗?

试试这个:

self.navigationController.navigationBar.translucent = NO;
于 2013-10-11T05:55:26.230 回答
2

尝试以下操作:

 Toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
于 2013-10-13T23:42:38.283 回答
1

尝试这个

CGRect frame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];

CGRect scrollFrame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
        UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];
于 2013-10-11T04:49:39.337 回答
1

我认为问题是由于滚动视图的内容插入自动设置不正确。

试试下面的。在您的loadView中,设置self.automaticallyAdjustsScrollViewInsetsNO(仅当NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)。现在,根据操作系统版本将contentInset和设置为正确的值:scrollIndicatorInsets

scrollview.contentInset = scrollview.scrollIndicatorInsets = UIEdgeInsetMake(NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1 ? 0 : 64, 0, 0, 0);
于 2013-10-13T01:24:02.060 回答
0

我的问题已解决,取消标记视图控制器中的“调整滚动视图插图”检查(使用情节提要)。

于 2014-05-12T17:36:10.777 回答