86234

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.

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

<img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/R6Cxo.png" alt="enter image description here">

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

<img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/BkRKT.png" alt="enter image description here">

But what I want is to have a picture like this. How do I get there?

<img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/PeqLY.png" alt="enter image description here">

Answer1:

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.

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), ])

Visit this link for more details, read pure auto layout approach

<strong>Update:</strong> 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 } } }

Recommend

  • Intercept/Programmatically set IBOutlet properties
  • move button towards target using constraints, no reaction?
  • UITableView row height not changing
  • How to add a minus sign to the .DecimalPad iOS keyboard?
  • How to center UILabel horizontally and vertically in screen using auto layout?
  • Multiple storyboards: should I use a singleton pattern to cache them?
  • “[CALayer release]: message sent to deallocated instance” when dismissing modal view controller
  • OSStatus error -50 (invalid parameters) AudioQueueNewInput recording audio on iOS
  • Why can't I extract data from media file using AVURLAsset in a Playground?
  • iOS 9 custom transition - animationControllerForDismissedController not called
  • UIScrollView setContentOffset: animated: not working
  • Why does Apple use assign rather than weak to store a delegate?
  • Should a web service response include empty values?
  • Jquery Mobile pageLoading() Method how does it work?
  • Application level floating views with navigation in Android
  • How to access profile picture with Facebook API in Swift 3?
  • UIAlertController button function not working
  • Combining two different ActiveRecord collections into one
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • How to remove a SwiftyJSON element?
  • D3 get axis values on zoom event
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Row Count Is Returning the incorrect number using RaptureXML
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Typescript - Unable to get 'import' statement to function
  • Read text file and split every line in MSBuild
  • Align navbar back button on right side
  • HTML download movie download link
  • Can Jackson SerializationFeature be overridden per field or class?
  • Obtain ObjectIdHex value from mgo query
  • How to show dropdown in excel using jrxml (jasper api)?
  • Traverse Array and Display in markup
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Bitwise OR returns boolean when one of operands is nil
  • python draw pie shapes with colour filled
  • How to push additional view controllers onto NavigationController but keep the TabBar?