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

dir*_*ill 5 objective-c uiscrollview uiview ios 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
  • 在viewDidLoad中创建并添加UIScrollView
  • 所有动态内容都在nib中绘制,并存储在单独的UIView名称contentView中
  • contentView被添加到viewDidLoad中的scrollView
  • 在viewDidLoad中添加了约束,将scrollView和contentView固定到superViews的边缘.

来自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]];

}
Run Code Online (Sandbox Code Playgroud)

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

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

dir*_*ill 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]];
Run Code Online (Sandbox Code Playgroud)

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