【问题标题】:iOS - Pure AutoLayout and UIScrollView not scrollingiOS - 纯 AutoLayout 和 UIScrollView 不滚动
【发布时间】:2014-10-24 15:28:49
【问题描述】:

这是我第一次使用带有纯自动布局方法的 UIScrollViews。这就是视图层次结构的样子

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

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

我将滚动视图的宽度、高度、中心和底部空间设置为超级视图。创建的 view1、view2 和 view3 都在其 updateConstraints 方法中设置了宽度和高度约束。此外,代码中提供了一些约束。这个滚动视图没有从左到右滚动的原因是什么?我已经阅读了我可以在网上找到的所有关于以编程方式使用自动布局创建子视图并将子视图添加到 UIScrollView 的指南。我发现有人提到必须为作为子视图添加到滚动视图的每个视图提供四种不同的约束,前导、尾随、顶部和底部。这些是唯一可以指定的 NSLayoutAttributes 吗? NSLayoutAttribueLeft 或 NSLayoutAttribueRight 等属性如何关联?我也阅读了 Apples 网站上的文档,特别是 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>
)

下面是它的截图:

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

【问题讨论】:

  • 尝试从 viewWillAppear 调用 initView,因为您的布局的所有尺寸都未在 viewDidLoad 中设置。
  • 这不会改变任何东西。我相信有一些限制没有被添加..我只是不知道是什么。

标签: ios ios7 uiscrollview autolayout


【解决方案1】:

确保 view1top_constraintview3bottom_constraint 符合 scrollView 的约束。否则滚动视图的contentSize: {0, 0}

【讨论】:

  • 我认为这是要考虑的最重要的事情!谢谢:)
【解决方案2】:

无论您在哪里打印约束,尝试打印 scrollview.contentSize,它可能是 0,0,这就是您的问题所在。据我所知,正如您在帖子中提到的那样,您必须将滚动视图的子视图显式设置为滚动视图的左上角和右下角约束。设置这些会自动设置滚动视图的 contentSize,使其能够滚动。看起来您只设置了 centerX 和 centerY 约束,它们不会将滚动视图 contentSize 设置为您需要的值。

尝试以编程方式设置这些(这是伪代码,但你明白了):

  • view1.topConstraint = scrollView.topConstraint
  • view1.leftConstraint = scrollView.leftConstraint
  • view3.bottomConstraint = scrollView.bottomConstraint
  • view3.rightConstraint = scrollView.rightConstraint

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多