Put a view at the end of UIScrollview - Swift - ios

My aim is to set a UIView at the end (right bottom) of a UIScrollView. I saw solution using UIContentSize but I don't want to use this property because it is not adapted : The UIView width isn't equal to the UIScrollview contentSize.
The UIView is a subview of the UIScrollView and I only need to scroll horizontally. I think I must use constraints in order to stick my UIView at the end of the UIscrollview (that means the UIView Right side is the same that the UIScrollView right side, both have the same height)
What I did is :
let horizontalConstraint = NSLayoutConstraint(item: self.myView , attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.myScrollView , attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
self.myScrollView.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: self.myView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.myScrollView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
self.myScrollView.addConstraint(verticalConstraint)
I'm a bit confused and stuck about how to set this constraints, I'm probably wrong because my view doesn't display anymore. Do I need to define all the constraint or only the one for the right and one for the top ? Any advice ? Thanks
EDIT :
The solution I want could be represented like this :

UIScrollView takes it's content size from it's subviews, so it will only be as large as what it contains. We can't set it's size independently. Everything should be in a subview.
Decide on the size of myScrollView you want:
let myViewWidth = myView.frame.width
let myViewHeight = myView.frame.height
let screenWidth = myScrollView.frame.width
let scrollContentWidth = myViewWidth + screenWidth
Create a subview, let's call it widthView
let widthView = UIView(frame: CGFloat(x: 0, y: 0, width: scrollContentWidth, height: myViewHeight))
myScrollView.addSubview(widthView)
let leadingEdgeConstraint = NSLayoutConstraint(item: widthView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: myScrollView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
myScrollView.addConstraint(leadingEdgeConstraint)
let trailingEdgeConstraint = NSLayoutConstraint(item: widthView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: myScrollView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
myScrollView.addConstraint(trailingEdgeConstraint)
let widthContainerConstraint = NSLayoutConstraint(item: widthView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: scrollContentWidth)
myScrollView.addConstraint(widthContainerConstraint)
Now add your myView to that view:
widthView.addSubview(myView)
myView.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraint = NSLayoutConstraint(item: myView , attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: widthView , attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
widthView.addConstraint(horizontalConstraint)
let heightConstraint = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: myViewHeight)
widthView.addConstraint(heightConstraint)
let widthConstraint = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: myViewWidth)
widthView.addConstraint(widthConstraint)

Thanks to informations provided by #Tim I finally managed this without using another Subview :
Solution :
// Create a view
myView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: self.myScrollView.frame.height))
myView.backgroundColor = UIColor.redColor()
self.myScrollView.addSubview(myView)
// constraints
myView.translatesAutoresizingMaskIntoConstraints = false
let trailingSpaceConstraint = NSLayoutConstraint(item: self.myScrollView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: myView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
myScrollView.addConstraint(trailingSpaceConstraint)
let screenWidth = self.view.frame.width
let leadingSpaceConstraint = NSLayoutConstraint(item: self.myView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.myScrollView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: screenWidth - 15)
myScrollView.addConstraint(leadingSpaceConstraint)
let widthConstraint = NSLayoutConstraint(item: self.myView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 100)
myScrollView.addConstraint(widthConstraint)
let bottomConstraint = NSLayoutConstraint(item: self.myScrollView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.myView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
myScrollView.addConstraint(bottomConstraint)
let topConstraint = NSLayoutConstraint(item: self.myView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.myScrollView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
myScrollView.addConstraint(topConstraint)
let heightConstraint = NSLayoutConstraint(item: self.myView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.myScrollView, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)
myScrollView.addConstraint(heightConstraint)

Related

UIScrollView is not scrolling after end of screen

I am trying to work with UIScrollView which I am adding programatically with auto layouts. I read other SO answers but none of them is working for me. My ScollView is scrolling but upto some point. After that end of the last child view is not showing. What i did was ,added a contentView to the UIScrollView and added subviews to the contentView. All the subviews are
translatesAutoresizingMaskIntoConstraints=false which is supposed t give me contentSize based on the constraints of the views but it is not. I tried locking the bottom of the contentView to the scrollview and also the bottom of the last subview of the contentview to the scrollview but it not working. Basically there are cards in the contentView which has height constraints. But they are not scrolling to the end. Last child is not scrolled to the end no matter what I do. What can be wrong with this approach?
class NodeSettingViewController: UIViewController,UIScrollViewDelegate {
var scrollView:UIScrollView?
var cellVM:NodeAppCellViewModel?
var contentView:UIView?
override func viewDidLoad() {
super.viewDidLoad()
let img = UIImage(named: "back_arrow")
let size = CGSize(width: 20, height: 20)
UIGraphicsBeginImageContext(size)
img!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let backButton = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: self, action: #selector(closeButtonTapped(_:)))
self.view.backgroundColor = UIColor.white
let appBar = MDCAppBarViewController()
appBar.headerStackView.backgroundColor = UIColor(netHex: Constants.color)
appBar.navigationBar.titleAlignment = .center
appBar.navigationBar.titleTextColor = UIColor.white
appBar.navigationBar.backItem = backButton
appBar.navigationBar.title = cellVM!.appName == nil ? "" : cellVM!.appName!
view.addSubview(appBar.view)
scrollView = UIScrollView()
scrollView!.translatesAutoresizingMaskIntoConstraints=false
contentView = UIView()
contentView!.backgroundColor = UIColor(netHex: Constants.BACKGROUUND_COLOR)
contentView!.translatesAutoresizingMaskIntoConstraints=false
let statusAndImageHolder = StatusAndImageHolderView()
statusAndImageHolder.translatesAutoresizingMaskIntoConstraints=false
statusAndImageHolder.setup(cellVM: cellVM)
contentView!.addSubview(statusAndImageHolder)
let healthCard = HealthCard()
healthCard.setShadowElevation(ShadowElevation.cardResting, for: UIControlState.normal)
healthCard.translatesAutoresizingMaskIntoConstraints=false
healthCard.setup(hubID: cellVM!.nodeApp!.hubId!, nodeID: cellVM!.nodeApp!.nodeId!,nApp: cellVM!.nodeApp)
let upperConstaintHealthCard = NSLayoutConstraint(item: healthCard, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentView!.subviews[contentView!.subviews.count-1], attribute: NSLayoutAttribute.bottom, multiplier: 1, constant:10)
let leftConstraintHealthCard = NSLayoutConstraint(item: healthCard, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let rightConstraintHealthCard = NSLayoutConstraint(item: healthCard, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.right, multiplier: 1, constant: -10)
let heightConstraintHealthCard = NSLayoutConstraint(item: healthCard, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 150)
contentView!.addSubview(healthCard)
let additionalInfoCard = AdditionalInformationCard()
additionalInfoCard.translatesAutoresizingMaskIntoConstraints=false
additionalInfoCard.setShadowElevation(ShadowElevation.cardResting, for: UIControlState.normal)
additionalInfoCard.setup()
let upperConstaintAdditionalInfoCard = NSLayoutConstraint(item: additionalInfoCard, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentView!.subviews[contentView!.subviews.count-1], attribute: NSLayoutAttribute.bottom, multiplier: 1, constant:10)
let leftConstraintAdditionalInfoCard = NSLayoutConstraint(item: additionalInfoCard, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let rightConstraintAdditionalInfoCard = NSLayoutConstraint(item: additionalInfoCard, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.right, multiplier: 1, constant: -10)
let heightConstraintAdditionalInfoCard = NSLayoutConstraint(item: additionalInfoCard, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 270)
contentView!.addSubview(additionalInfoCard)
let upperConstaintHolder = NSLayoutConstraint(item: statusAndImageHolder, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.top, multiplier: 1, constant:0)
let leftConstraintHolder = NSLayoutConstraint(item: statusAndImageHolder, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0)
let rightConstraintHolder = NSLayoutConstraint(item: statusAndImageHolder, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.right, multiplier: 1, constant: -0)
let heightConstraintHolder = NSLayoutConstraint(item: statusAndImageHolder, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 150)
scrollView!.addSubview(contentView!)
scrollView!.bringSubview(toFront: contentView!)
self.view.addSubview(scrollView!)
let contentSize = UIScreen.main.bounds.size.width
let contentHeight = UIScreen.main.bounds.size.height-30
let upperConstaintScrollView = NSLayoutConstraint(item: scrollView!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: appBar.view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let leftConstraintScrollView = NSLayoutConstraint(item: scrollView!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0)
let rightConstraintScrollView = NSLayoutConstraint(item: scrollView!, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.right, multiplier: 1, constant: -0)
let bottomConstraintScrollView = NSLayoutConstraint(item: scrollView!, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -0)
let upperConstaintContentView = NSLayoutConstraint(item: contentView!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: scrollView!, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
let leftConstraintContentView = NSLayoutConstraint(item: contentView!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: scrollView!, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0)
let widthConstraintContentView = NSLayoutConstraint(item: contentView!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentSize)
let heightConstraintContentView = NSLayoutConstraint(item: contentView!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentHeight)
NSLayoutConstraint.activate([upperConstaintScrollView,leftConstraintScrollView,rightConstraintScrollView,bottomConstraintScrollView,upperConstaintContentView,leftConstraintContentView,widthConstraintContentView,heightConstraintContentView,
upperConstaintHolder,leftConstraintHolder,rightConstraintHolder,heightConstraintHolder,
upperConstaintHealthCard,leftConstraintHealthCard,rightConstraintHealthCard,heightConstraintHealthCard,upperConstaintAdditionalInfoCard,leftConstraintAdditionalInfoCard,rightConstraintAdditionalInfoCard,heightConstraintAdditionalInfoCard])
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
#objc func closeButtonTapped(_ sender:Any){
self.dismiss(animated: true, completion: nil)
}
override func viewDidLayoutSubviews() {
scrollView?.isUserInteractionEnabled=true
scrollView!.isScrollEnabled=true
scrollView!.delegate=self
scrollView!.contentSize = contentView!.frame.size
}
}
I am sharing one of my card which is the last child of the subview.
class AdditionalInformationCard: MDCCard {
var titleLabel:UILabel?
var placeIdLabel:UILabel?
var eIDLabel:UILabel?
var hubIdLabel:UILabel?
var parentIDLabel:UILabel?
var deviceAddressLabel:UILabel?
var appIDLabel:UILabel?
var firmwareVersionLabel:UILabel?
var addedOnLabel:UILabel?
override func awakeFromNib() {
super.awakeFromNib()
}
func setup(){
let cardWidth = UIScreen.main.bounds.size.width-20
let contentWidth = cardWidth-20
titleLabel = UILabel()
titleLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
titleLabel?.textColor = UIColor(netHex: Constants.color)
titleLabel?.textAlignment = .left
titleLabel?.text = "Additional Information"
titleLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(titleLabel!)
placeIdLabel = UILabel()
placeIdLabel?.textColor = UIColor.gray
placeIdLabel?.text = "Place Id"
placeIdLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(placeIdLabel!)
eIDLabel = UILabel()
eIDLabel?.textColor = UIColor.gray
eIDLabel?.text = "eId"
eIDLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(eIDLabel!)
hubIdLabel = UILabel()
hubIdLabel?.textColor = UIColor.gray
hubIdLabel?.text = "HUB Id"
hubIdLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(hubIdLabel!)
parentIDLabel = UILabel()
parentIDLabel?.textColor = UIColor.gray
parentIDLabel?.text = "Parent Id"
parentIDLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(parentIDLabel!)
deviceAddressLabel = UILabel()
deviceAddressLabel?.textColor = UIColor.gray
deviceAddressLabel?.text = "Device Address"
deviceAddressLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(deviceAddressLabel!)
appIDLabel = UILabel()
appIDLabel?.textColor = UIColor.gray
appIDLabel?.text = "App Id"
appIDLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(appIDLabel!)
firmwareVersionLabel = UILabel()
firmwareVersionLabel?.textColor = UIColor.gray
firmwareVersionLabel?.text = "Firmware Version"
firmwareVersionLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(firmwareVersionLabel!)
addedOnLabel = UILabel()
addedOnLabel?.textColor = UIColor.gray
addedOnLabel?.text = "Added On"
addedOnLabel?.translatesAutoresizingMaskIntoConstraints=false
self.addSubview(addedOnLabel!)
let upperConstraintTitle = NSLayoutConstraint(item: titleLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 5)
let leftConstraintTitle = NSLayoutConstraint(item: titleLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let rightConstraintTitle = NSLayoutConstraint(item: titleLabel!, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 10)
let heightConstraintTitle = NSLayoutConstraint(item: titleLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
let upperConstraintPlaceId = NSLayoutConstraint(item: placeIdLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: titleLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 10)
let leftConstraintPlaceId = NSLayoutConstraint(item: placeIdLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let widthConstraintPlaceId = NSLayoutConstraint(item: placeIdLabel!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentWidth/2)
let heightConstraintPlaceId = NSLayoutConstraint(item: placeIdLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
let upperConstrainteId = NSLayoutConstraint(item: eIDLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: placeIdLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 8)
let leftConstrainteId = NSLayoutConstraint(item: eIDLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let widthConstrainteId = NSLayoutConstraint(item: eIDLabel!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentWidth/2)
let heightConstrainteId = NSLayoutConstraint(item: eIDLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
let upperConstraintHubIdLabel = NSLayoutConstraint(item: hubIdLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: eIDLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 8)
let leftConstraintHubIdLabel = NSLayoutConstraint(item: hubIdLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let widthConstraintHubIdLabel = NSLayoutConstraint(item: hubIdLabel!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentWidth/2)
let heightConstraintHubIdLabel = NSLayoutConstraint(item: hubIdLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
let upperConstraintParentIdLabel = NSLayoutConstraint(item: parentIDLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: hubIdLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 8)
let leftConstraintParentIdLabel = NSLayoutConstraint(item: parentIDLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let widthConstraintParentIdLabel = NSLayoutConstraint(item: parentIDLabel!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentWidth/2)
let heightConstraintParentIdLabel = NSLayoutConstraint(item: parentIDLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
let upperConstraintDeiveAddressLabel = NSLayoutConstraint(item: deviceAddressLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: parentIDLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 8)
let leftConstraintDeiveAddressLabel = NSLayoutConstraint(item: deviceAddressLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let widthConstraintDeiveAddressLabel = NSLayoutConstraint(item: deviceAddressLabel!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentWidth/2)
let heightConstraintDeiveAddressLabel = NSLayoutConstraint(item: deviceAddressLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
let upperConstraintAppId = NSLayoutConstraint(item: appIDLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: deviceAddressLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 8)
let leftConstraintAppId = NSLayoutConstraint(item: appIDLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let widthConstraintAppId = NSLayoutConstraint(item: appIDLabel!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentWidth/2)
let heightConstraintAppId = NSLayoutConstraint(item: appIDLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
let upperConstraintFirmwareVersion = NSLayoutConstraint(item: firmwareVersionLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: appIDLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 8)
let leftConstraintFirmwareVersion = NSLayoutConstraint(item: firmwareVersionLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let widthConstraintFirmwareVersion = NSLayoutConstraint(item: firmwareVersionLabel!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentWidth/2)
let heightConstraintFirmwareVersion = NSLayoutConstraint(item: firmwareVersionLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
let upperConstraintAddedOn = NSLayoutConstraint(item: addedOnLabel!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: firmwareVersionLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 8)
let leftConstraintAddedOn = NSLayoutConstraint(item: addedOnLabel!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let widthConstraintAddedOn = NSLayoutConstraint(item: addedOnLabel!, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: contentWidth/2)
let heightConstraintAddedOn = NSLayoutConstraint(item: addedOnLabel!, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20)
NSLayoutConstraint.activate([upperConstraintTitle,leftConstraintTitle,rightConstraintTitle,heightConstraintTitle,
upperConstraintPlaceId,leftConstraintPlaceId,widthConstraintPlaceId,heightConstraintPlaceId,
upperConstrainteId,leftConstrainteId,widthConstrainteId,heightConstrainteId,
upperConstraintHubIdLabel,leftConstraintHubIdLabel,widthConstraintHubIdLabel,heightConstraintHubIdLabel,
upperConstraintParentIdLabel,leftConstraintParentIdLabel,widthConstraintParentIdLabel,heightConstraintParentIdLabel,upperConstraintDeiveAddressLabel,leftConstraintDeiveAddressLabel,widthConstraintDeiveAddressLabel,heightConstraintDeiveAddressLabel,
upperConstraintAppId,leftConstraintAppId,widthConstraintAppId,heightConstraintAppId,
upperConstraintFirmwareVersion,leftConstraintFirmwareVersion,widthConstraintFirmwareVersion,heightConstraintFirmwareVersion,upperConstraintAddedOn,leftConstraintAddedOn,widthConstraintAddedOn,heightConstraintAddedOn])
}
}

Programatic Autolayout UI Elements

I am trying to autolayout 3 UI elements in the order which I present in the image. A UITextfield, UIDatePicker and a UIButton in a UIView.
I am avoiding to use storyboard as I want to get a better understanding of programmatic constraints and eventually use animations for them.
So far I have got this with some constraints I have tried:
and here is the code for the one I am working on:
override func viewDidLoad() {
super.viewDidLoad()
picker.translatesAutoresizingMaskIntoConstraints = false
picker.backgroundColor = UIColor.red
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor.blue
button.setTitle("Button", for: .normal)
self.view.addSubview(picker)
self.view.addSubview(button)
let PickercenterX = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
let PickercenterBottom = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.button, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -30)
let Pickerheight = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 150)
let Pickerwidth = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: self.view, attribute: NSLayoutAttribute.width, multiplier: 1, constant: -5)
// Centers it on the x axis. Pushes it it right if co constant has a value > 0
let centerX = NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
let centerBottom = NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -15)
let height = NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 50)
let width = NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: self.view, attribute: NSLayoutAttribute.width, multiplier: 1, constant: -15)
self.view.addConstraints([centerX, centerBottom, height, width, PickercenterX, PickercenterBottom, Pickerheight, Pickerwidth])
}
I am trying to work the button and date picker first before moving onto the textfield. How can I achieve this programmatically ?
Here lies the problem:-
You were setting the picker bottom to be equal to the button bottom with constant -30, although I know what were you trying to do, you were trying to give vertical space between picker and button. So it should be linked like, picker's bottom equal to button's top with constant -30.
Moreover you are missing out on activating the constraints by not adding isActive in the end.
Another way to activate all constraints at once is by using NSLayoutConstraint.activate() method
let PickercenterBottom = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.button, attribute: NSLayoutAttribute.top, multiplier: 1, constant: -30).isActive = true

UITextView and UIImageView in ScrollView

I want to make editable-textview and imageview both scroll. (Like Vesper app!) I have to do this programmatically.
I followed this post uitextview-inside-uiscrollview-with-autolayout
And I made this in code, but it doesn't scroll!
noteTextView.isScrollEnabled = false
//Adding views
view.addSubview(bScrollView)
bScrollView.addSubview(nView)
nView.addSubview(photoImageView)
nView.addSubview(noteTextView)
//The scrollview constraint
let Sleft = NSLayoutConstraint(item: bScrollView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
let Sright = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: bScrollView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)
let Sbottom = NSLayoutConstraint(item: bottomLayoutGuide, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: bScrollView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let Stop = NSLayoutConstraint(item: bScrollView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
//The view constraint
let Vleft = NSLayoutConstraint(item: nView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
let Vright = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: nView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)
let Vbottom = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: nView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let Vtop = NSLayoutConstraint(item: nView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
let Vequal = NSLayoutConstraint(item: nView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0)
//The textview constraint
let Nleft = NSLayoutConstraint(item: noteTextView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: nView, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
let Nright = NSLayoutConstraint(item: nView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: noteTextView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)
let Nbottom = NSLayoutConstraint(item: nView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: noteTextView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let Ntop = NSLayoutConstraint(item: noteTextView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: photoImageView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let Nheight = NSLayoutConstraint(item: noteTextView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 268)
//The imageview constraint
let Pleft = NSLayoutConstraint(item: photoImageView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: nView, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
let Pright = NSLayoutConstraint(item: nView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: photoImageView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)
let Ptop = NSLayoutConstraint(item: photoImageView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: nView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
let Pheight = NSLayoutConstraint(item: photoImageView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 300)
NSLayoutConstraint.activate([
Sleft, Sright, Sbottom, Stop,
Vleft, Vright, Vbottom, Vtop, Vequal,
Nleft, Nright, Nbottom, Ntop, Nheight,
Pleft, Pright, Ptop, Pheight
])
How can I make textView and imageView like Vesper app? Is there are another way?
You're code has some issues, try this:
class ViewController: UIViewController {
var bScrollView:UIScrollView!
var nView:UIView!
var photoImageView:UIImageView!
var noteTextView:UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.translatesAutoresizingMaskIntoConstraints = false
bScrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
bScrollView.backgroundColor = UIColor.black
nView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
nView.backgroundColor = UIColor.blue
photoImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
photoImageView.backgroundColor = UIColor.red
noteTextView = UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
noteTextView.backgroundColor = UIColor.yellow
noteTextView.clipsToBounds = true
bScrollView.translatesAutoresizingMaskIntoConstraints = false
nView.translatesAutoresizingMaskIntoConstraints = false
photoImageView.translatesAutoresizingMaskIntoConstraints = false
noteTextView.translatesAutoresizingMaskIntoConstraints = false
noteTextView.isScrollEnabled = false
//Adding views
view.addSubview(bScrollView)
bScrollView.addSubview(nView)
nView.addSubview(photoImageView)
nView.addSubview(noteTextView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//The scrollview constraint
let sLeading = NSLayoutConstraint(item: bScrollView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)
let sTrailing = NSLayoutConstraint(item: bScrollView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)
let sTop = NSLayoutConstraint(item: bScrollView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0)
let sBottom = NSLayoutConstraint(item: bScrollView, attribute: .bottom, relatedBy: .equal, toItem: bottomLayoutGuide, attribute: .bottom , multiplier: 1, constant: 0)
view.addConstraints([sLeading, sTrailing, sTop, sBottom])
//The view constraint
let vLeading = NSLayoutConstraint(item: nView, attribute: .leading, relatedBy: .equal, toItem: bScrollView, attribute: .leading, multiplier: 1, constant: 0)
let vTrailing = NSLayoutConstraint(item: nView, attribute: .trailing, relatedBy: .equal, toItem: bScrollView, attribute: .trailing, multiplier: 1, constant: 0)
let vTop = NSLayoutConstraint(item: nView, attribute: .top, relatedBy: .equal, toItem: bScrollView, attribute: .top, multiplier: 1, constant: 0)
let vBottom = NSLayoutConstraint(item: nView, attribute: .bottom, relatedBy: .equal, toItem: bScrollView, attribute: .bottom, multiplier: 1, constant: 0)
bScrollView.addConstraints([vLeading, vTrailing, vTop, vBottom])
let hEqual = NSLayoutConstraint(item: nView, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0)
let wEqual = NSLayoutConstraint(item: nView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: 0)
view.addConstraints([hEqual, wEqual])
//The imageview constraint
let pLeading = NSLayoutConstraint(item: photoImageView, attribute: .leading, relatedBy: .equal, toItem: nView, attribute: .leading, multiplier: 1, constant: 0)
let pTrailing = NSLayoutConstraint(item: photoImageView, attribute: .trailing, relatedBy: .equal, toItem: nView, attribute: .trailing, multiplier: 1, constant: 0)
let pTop = NSLayoutConstraint(item: photoImageView, attribute: .top, relatedBy: .equal, toItem: nView, attribute: .top, multiplier: 1, constant: 0)
let pHeight = NSLayoutConstraint(item: photoImageView, attribute: .height, relatedBy: .equal, toItem: nView, attribute: .width, multiplier: 1.0, constant: 0)
nView.addConstraints([pLeading, pTrailing, pTop, pHeight])
//The textview constraint
let txtLeading = NSLayoutConstraint(item: noteTextView, attribute: .leading, relatedBy: .equal, toItem: nView, attribute: .leading, multiplier: 1, constant: 0)
let txtTrailing = NSLayoutConstraint(item: noteTextView, attribute: .trailing, relatedBy: .equal, toItem: nView, attribute: .trailing, multiplier: 1, constant: 0)
let txtTop = NSLayoutConstraint(item: noteTextView, attribute: .top, relatedBy: .equal, toItem: photoImageView, attribute: .bottom, multiplier: 1, constant: 0)
let txtBottom = NSLayoutConstraint(item: noteTextView, attribute: .bottom, relatedBy: .equal, toItem: nView, attribute: .bottom, multiplier: 1.0, constant: 0)
nView.addConstraints([txtTop, txtLeading, txtTrailing, txtBottom])
view.layoutIfNeeded()
}
}

View hierarchy is not prepared for the constraint: Unable to Find the Issue

The Following are the constraints that am using.
<NSLayoutConstraint:0x7fcfdd405bc0 UILabel:0x7fcfdd405780'ACTIVITY'.centerY == UIView:0x7fcfdb677b60.centerY>The view hierarchy is not prepared for the constraint:
footer!.frame = CGRectMake(0, 0, tableView.frame.size.width, FOOTER_HEIGHT)
let activityLabel = UILabel()
activityLabel.text = "SomeText"
activityLabel.translatesAutoresizingMaskIntoConstraints = false
let CenterConstraint = NSLayoutConstraint(item: activityLabel, attribute: .CenterX, relatedBy: .Equal, toItem: footerView, attribute: .CenterX, multiplier: 1, constant: 0)
let CenterConstraint = NSLayoutConstraint(item: activityLabel, attribute: .CenterY, relatedBy: .Equal, toItem: footerView, attribute: .CenterY, multiplier: 1, constant: 0)
footer!.addConstraints([xxCenterConstraint,yyCenterConstraint])
footer!.addSubview(activityLabel)
let notificationLabel = UILabel()
notificationLabel.text = "9"
notificationLabel.textAlignment = .Center
notificationLabel.backgroundColor = customBlueColor
footerView!.addSubview(notificationLabel)
notificationLabel.translatesAutoresizingMaskIntoConstraints = false
let width = NSLayoutConstraint(item: notificationLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: NOTIFICATION_LABEL_HEIGHT)
let height = NSLayoutConstraint(item: notificationLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: NOTIFICATION_LABEL_HEIGHT)
let yCenterConstraint = NSLayoutConstraint(item: notificationLabel, attribute: .CenterY, relatedBy: .Equal, toItem: footerView, attribute: .CenterY, multiplier: 1, constant: 0)
let leadingConstraint1 = NSLayoutConstraint(item: activityLabel, attribute: .Trailing, relatedBy: .Equal, toItem: notificationLabel, attribute: .Leading, multiplier: 1, constant:-LEADING_SPACE)
footer!.addConstraints([width,height,yCenterConstraint,leadingConstraint1])
notificationLabel.layer.cornerRadius = NOTIFICATION_LABEL_HEIGHT/2
notificationLabel.clipsToBounds = true
You can't add constraints before the views are part of the same view hierarchy. Move the addSubview line before the addConstraints line.

ScrollView Won't Scroll AHHH

For the life of me I can't figure out what I'm doing wrong. All I'm trying to do is put a bunch of text fields in a scroll view and have it intrinsically determine the contentSize using auto layout. Unlike the other tutorials I've watched and read out there, I'm trying to do this programmatically, not through Storyboards or Xibs.
I've called setTranslatesAutoresizingMaskIntoConstraints(false) on every view in the scroll view (including on the scroll view itself).
I've set the height and width of the scroll view to match it's superview and even positioned the scroll view in the center of it's superview (I read that sometimes you need to do that to get it working)
I've pinned the content view's edges to the top, leading, trailing, and bottom edges of it's superview (the scroll view)
Here's my code:
class ViewController: UIViewController {
var scrollView = UIScrollView()
var contentView = UIView()
var textField1 = CustomTextField()
var textField2 = CustomTextField()
var textField3 = CustomTextField()
var textField4 = CustomTextField()
var textField5 = CustomTextField()
var textField6 = CustomTextField()
override func viewDidLoad() {
super.viewDidLoad()
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
textField1.setTranslatesAutoresizingMaskIntoConstraints(false)
textField2.setTranslatesAutoresizingMaskIntoConstraints(false)
textField3.setTranslatesAutoresizingMaskIntoConstraints(false)
textField4.setTranslatesAutoresizingMaskIntoConstraints(false)
textField5.setTranslatesAutoresizingMaskIntoConstraints(false)
textField6.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(textField1)
contentView.addSubview(textField2)
contentView.addSubview(textField3)
contentView.addSubview(textField4)
contentView.addSubview(textField5)
contentView.addSubview(textField6)
contentView.backgroundColor = .redColor()
setupConstraints()
}
func setupConstraints() {
// Install height and width constraints on our scroll view
let heightLC : NSLayoutConstraint = NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0)
let widthLC : NSLayoutConstraint = NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0)
self.view.addConstraints([heightLC,widthLC])
// Install centering constraints on our scroll view
let centerXLC : NSLayoutConstraint = NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
let centerYLC : NSLayoutConstraint = NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
self.view.addConstraints([centerXLC,centerYLC])
// Set the content view so it resizes with the same proportions on screen rotations //
let contentViewTopLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)
let contentViewLeadingLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
let contentViewTrailingLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
let contentViewBottomLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
let contentViewWidthLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0)
let contentViewHeightLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0)
scrollView.addConstraints([contentViewTopLC,contentViewLeadingLC,contentViewTrailingLC,contentViewBottomLC,contentViewWidthLC,contentViewHeightLC])
// Setup the text field constraints
let textField1Top : NSLayoutConstraint = NSLayoutConstraint(item: textField1, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)
let textField1Leading : NSLayoutConstraint = NSLayoutConstraint(item: textField1, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 40)
let textField1Trailing : NSLayoutConstraint = NSLayoutConstraint(item: textField1, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: -40)
contentView.addConstraints([textField1Top, textField1Leading, textField1Trailing])
let textField2Top : NSLayoutConstraint = NSLayoutConstraint(item: textField2, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textField1, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 100)
let textField2Leading : NSLayoutConstraint = NSLayoutConstraint(item: textField2, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 40)
let textField2Trailing : NSLayoutConstraint = NSLayoutConstraint(item: textField2, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: -40)
contentView.addConstraints([textField2Top, textField2Leading, textField2Trailing])
let textField3Top : NSLayoutConstraint = NSLayoutConstraint(item: textField3, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textField2, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 100)
let textField3Leading : NSLayoutConstraint = NSLayoutConstraint(item: textField3, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 40)
let textField3Trailing : NSLayoutConstraint = NSLayoutConstraint(item: textField3, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: -40)
contentView.addConstraints([textField3Top, textField3Leading, textField3Trailing])
let textField4Top : NSLayoutConstraint = NSLayoutConstraint(item: textField4, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textField3, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 100)
let textField4Leading : NSLayoutConstraint = NSLayoutConstraint(item: textField4, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 40)
let textField4Trailing : NSLayoutConstraint = NSLayoutConstraint(item: textField4, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: -40)
contentView.addConstraints([textField4Top, textField4Leading, textField4Trailing])
let textField5Top : NSLayoutConstraint = NSLayoutConstraint(item: textField5, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textField4, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 100)
let textField5Leading : NSLayoutConstraint = NSLayoutConstraint(item: textField5, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 40)
let textField5Trailing : NSLayoutConstraint = NSLayoutConstraint(item: textField5, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: -40)
contentView.addConstraints([textField5Top, textField5Leading, textField5Trailing])
let textField6Top : NSLayoutConstraint = NSLayoutConstraint(item: textField6, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textField5, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 100)
let textField6Leading : NSLayoutConstraint = NSLayoutConstraint(item: textField6, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 40)
let textField6Trailing : NSLayoutConstraint = NSLayoutConstraint(item: textField6, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: -40)
let textField6Bottom : NSLayoutConstraint = NSLayoutConstraint(item: textField6, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
contentView.addConstraints([textField6Top, textField6Leading, textField6Trailing, textField6Bottom])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
And here's my CustomTextField class I use for the text fields:
class CustomTextField: UITextField {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .whiteColor()
// set text field height to 55pts
let heightLC : NSLayoutConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 55)
addConstraint(heightLC)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Here's what the app currently looks like
(notice the 6th text field isn't being displayed and I can't scroll down to it):
Any help will greatly be appreciated :)
The problem is these lines:
let contentViewTopLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)
let contentViewLeadingLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
let contentViewTrailingLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
let contentViewBottomLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
let contentViewWidthLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0)
let contentViewHeightLC : NSLayoutConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0)
scrollView.addConstraints([contentViewTopLC,contentViewLeadingLC,contentViewTrailingLC,contentViewBottomLC,contentViewWidthLC,contentViewHeightLC])
What you want is not for the content view to be sized like the scroll view at all. That is exactly what is keeping the scroll view from scrolling. You want the content view's four edges to be pinned to the four edges of the scroll view.
That way, the contents of the content view will push outwards on the content view, sizing it. It will thus try to become bigger than the scroll view. But it can't, because you're also going to pin the sides of the scroll view to its superview. Thus, the content view's size will become the bounds size of the scroll view - its contentSize. When the contentSize of the scroll view is thus bigger than the scroll view, it will be scrollable.

Resources