简体   繁体   English

NSLayoutConstraint可以将视图固定到superview的下边缘

[英]NSLayoutConstraint that would pin a view to the bottom edge of a superview

A reproducible example 一个可重复的例子

class ViewController: UIViewController {

   var created = false

   override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      if !created {

         let scrollView = UIScrollView()
         scrollView.backgroundColor = UIColor.grayColor()
         view.addSubview(scrollView)

         let kidView = UIView()
         kidView.backgroundColor = UIColor.redColor()
         kidView.translatesAutoresizingMaskIntoConstraints = false

         scrollView.addSubview(kidView)

         scrollView.translatesAutoresizingMaskIntoConstraints = false
         kidView.translatesAutoresizingMaskIntoConstraints = false

         view.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: .AlignAllLeft, metrics: nil, views: ["scrollView": scrollView])
         )
         view.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: .AlignAllLeft, metrics: nil, views: ["scrollView": scrollView])
         )

         kidView.addConstraints([
            NSLayoutConstraint(item: kidView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100),
            NSLayoutConstraint(item: kidView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100),
         ])

         scrollView.addConstraints([
            NSLayoutConstraint(item: kidView, attribute: .CenterX, relatedBy: .Equal, toItem: scrollView, attribute: .CenterX, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: kidView, attribute: .Bottom, relatedBy: .Equal, toItem: scrollView, attribute: .Bottom, multiplier: 1, constant: 0),
//            NSLayoutConstraint(item: kidView, attribute: .CenterY, relatedBy: .Equal, toItem: scrollView, attribute: .CenterY, multiplier: 1, constant: 0),
         ])

         created = true
      }
   }

}

Problem 问题

I want to align my custom view to the bottom edge of my UIScrollView , but I can only seem to align it to the top, or vertical center. 我想将我的自定义视图对齐到我的UIScrollView的底部边缘,但我似乎只能将它与顶部或垂直中心对齐。

scrollView.addConstraints([
    NSLayoutConstraint(
        item: circleContainerView, 
        attribute: .Top, 
        relatedBy: .Equal, 
        toItem: scrollView, 
        attribute: .Top, 
        multiplier: 1, 
        constant: 0),
])

在此输入图像描述

scrollView.addConstraints([
    NSLayoutConstraint(
        item: circleContainerView, 
        attribute: .CenterY, 
        relatedBy: .Equal, 
        toItem: scrollView, 
        attribute: .CenterY, 
        multiplier: 1, 
        constant: 0),
])

在此输入图像描述

But what I want is to have a picture like this. 但我想要的是拥有这样的照片。 How do I get there? 我如何到达那里?

在此输入图像描述

ScrollView with auto layout works differently either you can use only one subview by setting translatesAutoresizingMaskIntoConstraints = true and setting contentSize explicitly. 具有自动布局的ScrollView的工作方式不同,您可以通过设置translatesAutoresizingMaskIntoConstraints = true并显式设置contentSize来仅使用一个子视图。 Or you set translatesAutoresizingMaskIntoConstraints = false and let it find out constraint it self. 或者你设置translatesAutoresizingMaskIntoConstraints = false并让它找出它自己的约束。

In your case you can add an invisible view inside scroll and pin it to top and set its height to scrollView.bounds.size.height and then set create constraint with that invisible view 在您的情况下,您可以在滚动内添加一个不可见的视图并将其固定到顶部并将其高度设置为scrollView.bounds.size.height然后使用该不可见视图设置创建约束

Change your constraint like this 像这样改变你的约束

scrollView.addConstraints([
    NSLayoutConstraint(
        item: circleContainerView, 
        attribute: .Bottom, 
        relatedBy: .Equal, 
        toItem: invisibleView, 
        attribute: .Bottom, 
        multiplier: 1, 
        constant: 0),
])

Visit this link for more details, read pure auto layout approach 有关更多详细信息,请访问此链接 ,阅读纯自动布局方法

Update: your modified code 更新:修改后的代码

class ViewController: UIViewController {

    var created = false

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if !created {

            let scrollView = UIScrollView()
            scrollView.backgroundColor = UIColor.grayColor()
            view.addSubview(scrollView)

            let kidView = UIView()
            kidView.backgroundColor = UIColor.redColor()

            scrollView.addSubview(kidView)

            scrollView.translatesAutoresizingMaskIntoConstraints = false
            kidView.translatesAutoresizingMaskIntoConstraints = false

            scrollView.frame = view.bounds;
            scrollView.contentSize = view.bounds.size

            // Add an invisible view

            let inV = UIView()
            inV.backgroundColor = UIColor.clearColor()
            inV.translatesAutoresizingMaskIntoConstraints = false
            scrollView.addSubview(inV)


            view.addConstraints(
                NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: .AlignAllLeft, metrics: nil, views: ["scrollView": scrollView])
            )
            view.addConstraints(
                NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: .AlignAllLeft, metrics: nil, views: ["scrollView": scrollView])
            )

            var constraint = NSLayoutConstraint(item: inV, attribute: .Top, relatedBy: .Equal, toItem: scrollView, attribute: .Top, multiplier: 1, constant: 0)

            view.addConstraint(constraint)

            constraint = NSLayoutConstraint(item: inV, attribute: .Left, relatedBy: .Equal, toItem: scrollView, attribute: .Left, multiplier: 1, constant: 0)
            view.addConstraint(constraint)

            inV.addConstraints([
                NSLayoutConstraint(item: inV, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: 10),
                NSLayoutConstraint(item: inV, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: self.view.bounds.size.height),
                ])

            //

            kidView.addConstraints([
                NSLayoutConstraint(item: kidView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: 100),
                NSLayoutConstraint(item: kidView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: 100),
                ])

            view.addConstraints([
                NSLayoutConstraint(item: kidView, attribute: .CenterX, relatedBy: .Equal, toItem: scrollView, attribute: .CenterX, multiplier: 1, constant: 0),
                NSLayoutConstraint(item: kidView, attribute: .Bottom, relatedBy: .Equal, toItem: inV, attribute: .Bottom, multiplier: 1, constant: 0),
                ])

            created = true
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将视图扩展到超级视图的底部 - Extend view to bottom of superview NSLayoutConstraint - 超级视图的动态高度 - NSLayoutConstraint - dynamic height of a superview 将 UIScrollView 中的内容视图固定到其底部而不是其顶部边缘 - Pin content view in UIScrollView to its bottom rather than its top edge 在viewboard中将view.bottom约束到superview.bottom而不是bottomLayoutGuide - constrain view.bottom to superview.bottom not bottomLayoutGuide in storyboard 如何获得视图的底部边缘? - How to get bottom edge of the view? 尝试将UIScrollView固定在超级视图的底部时,不会遍及整个屏幕 - UIScrollView don't go all over the screen when trying to pin it to the bottom of the superview 如何将UIView固定到其超级视图的底部,但是当出现键盘时将其移动? - How to pin a UIView to the bottom of its superview, but have it move when the keyboard appears? Swift VFL将商品固定在距Superview底部边距固定距离的位置 - Swift VFL to pin an item a fixed distance from superview's bottom margin 如何将按钮固定到视图Storyboard iOS的底部 - How to pin button to bottom of view Storyboard iOS 使用NSLayoutConstraint在Superview中使用UIPageControl进行编程垂直对齐 - Programmatic Vertical Alignment of UIPageControl in Superview using NSLayoutConstraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM