iOS - 纯AutoLayout和UIScrollView不滚动

问题描述:

这是我第一次使用UIScrollViews纯粹的Autolayout方法。这是视图层次结构的样子iOS - 纯AutoLayout和UIScrollView不滚动

view 
-scrollview 
--view1 
--view2 
--view3 

scrollview应该按顺序包含view1 | view2 | view3。

我将scrollviews的宽度,高度,centerx和底部空间设置为superview。创建的view1,view2和view3都在其updateConstraints方法中设置了宽度和高度约束。此外,代码中提供了一些限制。这个scrollview不是从左到右滚动的原因是什么?从字面上看,我可以在网上找到关于以自动布局以编程方式创建和添加子视图到UIScrollView的所有指南。我发现一些提到必须为每个视图提供四个不同的约束,前导,尾随,顶部和底部作为子视图添加到滚动视图。这些是唯一可以指定的NSLayoutAttributes吗?属性如NSLayoutAttribueLeft或NSLayoutAttribueRight如何相关?我也阅读过苹果网站上的文档,特别是https://developer.apple.com/library/ios/technotes/tn2154/_index.html。我附加了我目前拥有的设置。一切都是通过代码完成的。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.dataSource = @[ [[PCCGenericRating alloc] initWithTitle:@"Easiness" 
                 andMessage:@"WHAT A JOKERRRR" 
                andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]], 

         [[PCCGenericRating alloc] initWithTitle:@"Joker" 
                 andMessage:@"WHAT A JOKERRRR" 
                andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]], 

         [[PCCGenericRating alloc] initWithTitle:@"Difficulty" 
                 andMessage:@"YOu are not difficult at all" 
                andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]] 
         ]; 
    [self initView]; 
} 

- (void)initView { 
    CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height; 
    CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height; 

    CGFloat heightDifference = navigationBarHeight + statusBarHeight; 

    self.scrollView = [[UIScrollView alloc] init]; 
    self.scrollView.delegate = self; 
    [self.scrollView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    self.scrollView.backgroundColor = [UIColor greenColor]; 
    [self.view addSubview:self.scrollView]; 


    //setup constraints 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeWidth 
                        relatedBy:NSLayoutRelationEqual 
                         toItem:self.view 
                        attribute:NSLayoutAttributeWidth 
                        multiplier:1.0f 
                        constant:0.0f]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeHeight 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
                  attribute:NSLayoutAttributeHeight 
                 multiplier:1.0f 
                  constant:-heightDifference]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeCenterX 
                        relatedBy:NSLayoutRelationEqual 
                         toItem:self.view 
                        attribute:NSLayoutAttributeCenterX 
                        multiplier:1.0f 
                        constant:0.0f]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeBottom 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
                  attribute:NSLayoutAttributeBottom 
                 multiplier:1.0f 
                  constant:0.0]]; 

    [self.dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
     PCCGenericRating *rating = (PCCGenericRating *)obj; 
     PCCGenericRatingView *ratingView = [self createViewWithRating:rating]; 
     [self.scrollView addSubview:ratingView]; 

     int multiplier = (idx == 0) ? 1 : (int) (idx + 1) ; 
     [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:ratingView 
                     attribute:NSLayoutAttributeCenterX 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:self.scrollView 
                     attribute:NSLayoutAttributeCenterX 
                     multiplier:multiplier 
                     constant:0.0f]]; 

     [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:ratingView 
                    attribute:NSLayoutAttributeCenterY 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:self.scrollView 
                    attribute:NSLayoutAttributeCenterY 
                    multiplier:1.0f 
                    constant:0.0f]]; 
    }]; 
} 

- (PCCGenericRatingView *)createViewWithRating:(PCCGenericRating *)rating { 
    PCCGenericRatingView *view = [PCCGenericRatingView genericRatingViewWithTitle:rating.title andMessage:rating.message]; 
    return view; 
} 

在打印出了滚动的限制,他们看起来还好我:

po self.scrollView.constraints 
<__NSArrayM 0x115b051f0>(
<NSLayoutConstraint:0x1145d9290 PCCGenericRatingView:0x114579880.centerX == UIScrollView:0x11458d4b0.centerX>, 
<NSLayoutConstraint:0x1145d9410 PCCGenericRatingView:0x114579880.centerY == UIScrollView:0x11458d4b0.centerY>, 
<NSLayoutConstraint:0x1145d9dd0 PCCGenericRatingView:0x1145d9560.centerX == 2*UIScrollView:0x11458d4b0.centerX>, 
<NSLayoutConstraint:0x1145d9e40 PCCGenericRatingView:0x1145d9560.centerY == UIScrollView:0x11458d4b0.centerY>, 
<NSLayoutConstraint:0x1145da6b0 PCCGenericRatingView:0x1145d9e90.centerX == 3*UIScrollView:0x11458d4b0.centerX>, 
<NSLayoutConstraint:0x1145da730 PCCGenericRatingView:0x1145d9e90.centerY == UIScrollView:0x11458d4b0.centerY> 
) 

这里是什么样子的截图: simulator

我觉得很奇怪,最后数据源中的元素是第一个显示在滚动视图中的视图控制器,它应该是最后一个视图。它也不会从左到右滚动。

+0

尝试从viewWillAppear调用initView,因为您的布局的所有维度都未在viewDidLoad中设置。 – 2014-08-31 11:21:41

+0

这不会改变任何东西。我相信有一些约束是不会被添加的。我只是不知道是什么。 – kamran619 2014-08-31 17:37:34

无论您在何处打印您的约束,请尝试打印scrollview.contentSize,它可能会是0,0,这就是您的问题所在。据我所知,正如你在文章中提到的那样,你必须明确地将scrollview的子视图设置为滚动视图左上和右下限制。设置这些会自动设置滚动视图的contentSize,使其能够滚动。它看起来像你只设置centerX和centerY约束,它不会将scrollviews contentSize设置为你所需要的。

尝试编程设置这些(这是伪代码,但你的想法):

  • view1.topConstraint = scrollView.topConstraint
  • view1.leftConstraint = scrollView.leftConstraint
  • view3.bottomConstraint =滚动视图.bottomConstraint
  • view3.rightConstraint = scrollView.rightConstraint

如果您将所有这些设置正确,您的滚动视图将正确滚动。请记住检查内容,如果contentsize是0,0,那么您的约束没有正确设置。

确保您top_constraint厂景bottom_constraintVIEW3会按您的滚动视图的约束。否则滚动查看的contentSize: {0, 0}

+0

我认为这是最重要的考虑!谢谢 :) – Shardul 2015-08-12 16:14:53