【问题标题】:Center or pin to top using autolayout使用自动布局居中或固定到顶部
【发布时间】:2013-11-01 15:19:30
【问题描述】:

我有一个 UIScrollView,它可以有不同高度的内容视图。如果内容小于scrollView,我希望内容垂直居中。但是,当内容较大时,我希望它保持在顶部。

这可以使用自动布局来实现吗?

【问题讨论】:

    标签: ios cocoa-touch uiscrollview autolayout


    【解决方案1】:

    我发现您可以设置一个使视图居中的约束,然后设置一个具有更高优先级的约束,即内容顶部只能大于 0。

    // If the content is smaller than the scrollview then center it, else lock to top
    NSLayoutConstraint *centerYConstraint = [NSLayoutConstraint constraintWithItem:self.contentController.view
                                                                         attribute:NSLayoutAttributeCenterY
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:_scrollView
                                                                         attribute:NSLayoutAttributeCenterY
                                                                        multiplier:1 constant:0];
    
    // Constrain the top to not be smaller than 0 (multiplier:0)
    NSLayoutConstraint *lockToTopConstraint = [NSLayoutConstraint constraintWithItem:self.contentController.view
                                                                           attribute:NSLayoutAttributeTop
                                                                           relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                                              toItem:_scrollView
                                                                           attribute:NSLayoutAttributeTop
                                                                          multiplier:0 constant:0];
    //It't more important that the content doesn't go over the top than that it is centered
    centerYConstraint.priority = UILayoutPriorityDefaultLow;
    lockToTopConstraint.priority = UILayoutPriorityDefaultHigh;
    [self.view addConstraints:@[centerYConstraint, lockToTopConstraint]];
    

    【讨论】:

      【解决方案2】:

      刚刚开始工作,可能有更好的方法,但这似乎工作得很好。

      1. 在滚动视图中设置内容视图,如下所示。

      2. 为 3 个约束添加 IBOutlets,顶部的 VerticalSpace,底部的垂直空间,然后是内容视图的高度。

      3。 ` -(无效)调整内容大小 {

      self.contentHeight.constant = 1000; //I tried out different values here to make sure it'd work for whatever size you need.
      
      if (self.contentHeight.constant > self.view.frame.size.height)
      {
          self.contentVerticalSpaceTop.constant = 0; //This will ensure it sticks to the top if it's bigger than the frame
      }else
      {
          self.contentVerticalSpaceTop.constant = ((self.view.frame.size.height/2)-(self.contentHeight.constant/2));
      }
      self.contentBottomVerticalSpace.constant = self.contentVerticalSpaceTop.constant; //setting the bottom constraint's constant to the top's will ensure that the content's centered vertically 
      [self.view layoutIfNeeded];
      

      } `

      【讨论】:

      • 谢谢,这很有帮助!
      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 2019-06-15
      • 2023-03-18
      • 2014-06-23
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多