本文介绍了NSLayoutConstraint将pin视图到一个superview的底部边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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 } }} 问题 我的自定义视图到我的 UIScrollView 的底部边缘,但我只能似乎将它对齐到顶部或垂直中心。ProblemI 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.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工作方式不同,您可以通过设置 translatesAutoresizingMaskIntoConstraints = true 并显式设置contentSize,只使用一个子视图。 或者你设置 translatesAutoresizingMaskIntoConstraints = false ,让它自己找出约束。ScrollView with auto layout works differently either you can use only one subview by setting translatesAutoresizingMaskIntoConstraints = true and setting contentSize explicitly.Or you set translatesAutoresizingMaskIntoConstraints = false and let it find out constraint it self.您可以在滚动内添加一个不可见的视图并将其固定到顶部,并将其高度设置为 scrollView.bounds.size.height ,然后使用该不可见视图设置创建约束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更改您的约束,如下所示Change your constraint like this scrollView.addConstraints([ NSLayoutConstraint( item: circleContainerView, attribute: .Bottom, relatedBy: .Equal, toItem: invisibleView, attribute: .Bottom, multiplier: 1, constant: 0),]) a href =https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-6_0/index.html =nofollow>链接了解详情,阅读纯自动布局方法Visit this link for more details, read pure auto layout approach 更新:您修改的代码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 } }} 这篇关于NSLayoutConstraint将pin视图到一个superview的底部边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 19:24