AutoLayout UIScrollView内容在旋转时不调整大小(包括示例项目)

时间:2013-12-27 19:09:46

标签: ios objective-c uiview uiscrollview autolayout

我正试图绕过AutoLayout的工作,我想除了UIScrollViews之外我对它的理解大部分都是如此。当我将这些视图添加到项目时,他们拒绝扩展屏幕的尺寸。它们在iPhone垂直视图中显得很好,但在旋转时,它们不会扩展。此外,当您在iPad模拟器中启动项目时,UIScrollView屏幕将不会扩展到iPad的尺寸。实际的UIScrollView会扩展,但内容不会扩展。

我已按照Apple(https://developer.apple.com/library/ios/technotes/tn2154/_index.html)的说明操作,虽然修复了我的动态内容高度问题,但它还没有解决UIScrollView内容未展开以匹配屏幕宽度的问题。我已经读过,我需要将scrollview的内部子项固定到UIView的右边缘,但这看起来有点像黑客,这似乎与上面的Apple文档相矛盾。

这就是我所拥有的:

  • UI因此主视图是在笔尖中创建的。
  • 视图是常规的UIView。不是UIScrollView
  • 创建UIScrollView并将其添加到viewDidLoad
  • 所有动态内容都在笔尖中绘制,并存储在单独的UIView名称contentView
  • contentView将添加到viewDidLoad中的scrollView
  • 在viewDidLoad中添加了约束,将scrollView和contentView固定到超级视图的边缘。

来自viewDidLoad的代码:

- (void)viewDidLoad
{
  [super viewDidLoad];

  UIView* contentView = self.contentView;

  UIScrollView * scrollView = [[UIScrollView alloc] init];

  [self.view addSubview:scrollView];
  [scrollView addSubview:contentView];

  //remove auto contraints
  [scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
  [contentView setTranslatesAutoresizingMaskIntoConstraints:NO];

  // Set the constraints for the scroll view and the image view.
  NSDictionary* viewsDictionary = NSDictionaryOfVariableBindings(scrollView, contentView);
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: nil views:@{@"scrollView":scrollView}]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: nil views:@{@"scrollView":scrollView}]];
  [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics: nil views:viewsDictionary]];
  [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics: nil views:viewsDictionary]];

}

您可以从此处下载示例项目:TestAutoLayout.zip

有没有人知道为什么尽管添加了约束,scrollView内容却没有扩展到self.view的宽度?

2 个答案:

答案 0 :(得分:10)

正如本答案中所解释的:https://stackoverflow.com/a/16843937/950953,将contentView链接到主视图的宽度似乎是唯一可行的。

我用这段代码修改了上面的约束代码,现在屏幕正确显示。

UIView *mainView = self.view;

// Set the constraints for the scroll view and the image view.
NSDictionary* viewsDictionary = NSDictionaryOfVariableBindings(scrollView, contentView, mainView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics: 0 views:viewsDictionary]];

//hack to tie contentView width to the width of the screen
[mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[contentView(==mainView)]" options:0 metrics:0 views:viewsDictionary]];

如果有人能找到合适的解决方案,请发布。

答案 1 :(得分:1)

我试图做类似的事情。我最终尝试了你所使用的“黑客”,但它对我不起作用,因为我遇到了某种约束冲突。但是我终于能够通过添加一行来实现它的工作:

[contentView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
相关问题