Unable to simultaneously satisfy constraints when adding any non-zero constraint programmatically - ios

import UIKit
#IBDesignable
class LargeButtonWithIcon: UIView {
var iconBackgroundView: UIView?
var iconIv: UIImageView?
#IBInspectable var iconImage: UIImage? {
didSet {
self.iconIv?.image = iconImage
}
}
override init(frame: CGRect) {
super.init(frame: frame)
initializeView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initializeView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
initializeView()
}
private func initializeView() {
initializeIconView()
}
private func initializeIconView() {
iconBackgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.height, height: self.frame.height))
addSubview(iconBackgroundView!)
iconBackgroundView?.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
iconBackgroundView?.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
iconBackgroundView?.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
iconIv = UIImageView()
iconIv?.image = iconImage
iconIv?.clipsToBounds = true
iconIv?.contentMode = .scaleAspectFit
iconIv?.translatesAutoresizingMaskIntoConstraints = false
iconBackgroundView!.addSubview(iconIv!)
iconBackgroundView?.leadingAnchor.constraint(equalTo: iconBackgroundView!.leadingAnchor, constant: 20).isActive = true
// iconBackgroundView?.topAnchor.constraint(equalTo: iconBackgroundView!.topAnchor, constant: 0).isActive = true
// iconBackgroundView?.rightAnchor.constraint(equalTo: iconBackgroundView!.rightAnchor, constant: 20).isActive = true
// iconBackgroundView?.bottomAnchor.constraint(equalTo: iconBackgroundView!.bottomAnchor, constant: 20).isActive = true
// iconBackgroundView?.centerXAnchor.constraint(equalTo: iconBackgroundView!.centerXAnchor).isActive = true
// iconBackgroundView?.centerYAnchor.constraint(equalTo: iconBackgroundView!.centerYAnchor).isActive = true
}
}
I'm trying to achieve the following:
iconBackgroundView is the square at the left of the button, iconIv is the icon in that square.
When I activate any non-zero constraint for the iconIv (or all of them), it prints the warning:
2020-06-03 01:26:37.163392+0300 FindDifferences[5310:34823037] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x6000012bc6e0 UIView:0x7f880c0075a0.leading == UIView:0x7f880c0075a0.leading + 20 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000012bc6e0 UIView:0x7f880c0075a0.leading == UIView:0x7f880c0075a0.leading + 20 (active)>
But if I change the its constant to 0, it does not. Why, and how should I center the iconIv in this view with given padding?

This line doesn't make sense because you're trying to set a view's leading anchor equal to its own leading anchor.
iconBackgroundView?.leadingAnchor.constraint(equalTo: iconBackgroundView!.leadingAnchor, constant: 20).isActive = true
I think what you actually want is this:
iconIv?.leadingAnchor.constraint(equalTo: iconBackgroundView!.leadingAnchor, constant: 20).isActive = true

Related

Cannot move UILabel position programatically

I have a file for the View section of app, where i have all the labels and images that i intent to use, this is what i have in my DetailViewTableCell class, which inherits from UIView.
class DetailViewTableCell: UIView {
var detailMainImage: UIImageView = UIImageView()
var detailName: UILabel = UILabel()
var detailType: UILabel = UILabel()
var detailHeart: UIImageView = UIImageView()
}
Now i move to my DetailViewController class, here i try and add the label, the label is added but it appears always at top left corner at 0,0 coordinate, when i try and add constraints for position, i always get error, now i can try
detailMain.detailName.frame.origin.x = 30
but i get error:
Constraint items must each be a view or layout guide.
In any case i do not wish to use this approach but more something like this
NSLayoutConstraint(item: detailMain.detailName, attribute: .leading, relatedBy: .equal, toItem: detailMain.detailName.superview, attribute: .leading, multiplier: 1, constant: 20).isActive = true
but i get the same above error, my over all code is this:
self.view.addSubview(detailMain.detailName)
detailMain.detailName.translatesAutoresizingMaskIntoConstraints = false
detailMain.detailName.heightAnchor.constraint(equalToConstant: 25).isActive = true
detailMain.detailName.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
detailMain.detailName.font = UIFont(name: "Rubik-Medium", size: 30)
detailMain.detailName.backgroundColor = UIColor.white
detailMain.detailName.textColor = UIColor.black
Which works perfectly fine but the moment i try and constraints, the error come up, this is how the app shows up with out constraints and name at top most left corner
////////UPDATE
So here is my new DetailViewTableCell,
import UIKit
class DetailViewTableCell: UIView {
var detailMainImage: UIImageView = UIImageView()
var detailName: UILabel = UILabel()
var detailType: UILabel = UILabel()
var detailHeart: UIImageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
[detailMainImage, detailName, detailType, detailHeart].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
addSubview($0)
}
NSLayoutConstraint.activate([
// constrain main image to all 4 sides
detailMainImage.topAnchor.constraint(equalTo: topAnchor),
detailMainImage.leadingAnchor.constraint(equalTo: leadingAnchor),
detailMainImage.trailingAnchor.constraint(equalTo: trailingAnchor),
detailMainImage.bottomAnchor.constraint(equalTo: bottomAnchor),
// activate the height contraint
// constrain detailType label
// 30-pts from Leading
// 12-pts from Top
detailType.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 30.0),
detailType.topAnchor.constraint(equalTo: topAnchor, constant: 12.0),
// constrain detailName label
// 30-pts from Leading
// 12-pts from Bottom
detailName.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 30.0),
detailName.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12.0),
// constrain detailHeart image
// 12-pts from Trailing
// 12-pts from Bottom
// width: 24 height: equal to width (1:1 square)
detailHeart.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12.0),
detailHeart.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12),
detailHeart.widthAnchor.constraint(equalToConstant: 24),
detailHeart.heightAnchor.constraint(equalTo: detailHeart.widthAnchor)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and this 2 lines is what i add to my Detail view controller in viewDidLoad
let v = DetailViewTableCell()
detailTableView.tableHeaderView = v
Also i add this function
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// this is needed to allow the header view's content
// to determine its height
guard let headerView = detailTableView.tableHeaderView else {
return
}
let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
if headerView.frame.size.height != size.height {
headerView.frame.size.height = size.height
detailTableView.tableHeaderView = headerView
detailTableView.layoutIfNeeded()
}
}
then in my viewForHeaderInSection inbuilt function i add this
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
tableView.rowHeight = 80
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
headerView.heightAnchor.constraint(equalToConstant: 400).isActive = true
detailMain.detailMainImage.translatesAutoresizingMaskIntoConstraints = false
detailMain.detailMainImage.heightAnchor.constraint(equalToConstant: 400).isActive = true
detailMain.detailMainImage.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
detailMain.detailMainImage.image = UIImage(named: restaurant.image)
detailMain.detailMainImage.contentMode = .scaleAspectFill
detailMain.detailMainImage.clipsToBounds = true
headerView.addSubview(detailMain.detailMainImage)
//Add the name
detailMain.detailName.text = restaurant.name
headerView.addSubview(detailMain.detailName)
return headerView
}
but still same position , is there any thing i add to add or remove from my code
You didn't show where you *want the labels, but this should get you going...
In your "header view" class:
add your elements: detailMainImage, detailName, etc...
set their properties and constraints as desired
You can get auto-layout to use the constraints you've setup in the header view to automatically determine its height:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// this is needed to allow the header view's content
// to determine its height
guard let headerView = tableView.tableHeaderView else {
return
}
let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
if headerView.frame.size.height != size.height {
headerView.frame.size.height = size.height
tableView.tableHeaderView = headerView
tableView.layoutIfNeeded()
}
}
So, here's a complete example:
class TestHeaderTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// instantiate the header view
let v = DetailTableHeaderView()
// set it as the tableHeaderView
tableView.tableHeaderView = v
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// this is needed to allow the header view's content
// to determine its height
guard let headerView = tableView.tableHeaderView else {
return
}
let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
if headerView.frame.size.height != size.height {
headerView.frame.size.height = size.height
tableView.tableHeaderView = headerView
tableView.layoutIfNeeded()
}
}
}
class DetailTableHeaderView: UIView {
var detailMainImage: UIImageView = UIImageView()
var detailName: UILabel = UILabel()
var detailType: UILabel = UILabel()
var detailHeart: UIImageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
backgroundColor = .white
// give the heart image view a background color so we can see its frame
detailHeart.backgroundColor = .red
if let img = UIImage(named: "teacup") {
detailMainImage.image = img
}
// give labels some text so we can see them
detailType.text = "Detail Type"
detailName.text = "Detail Name"
// setup fonts for labels as desired
//detailName.font = UIFont(name: "Rubik-Medium", size: 30)
// I don't have "Rubik" so this is with the system font
detailName.font = UIFont.systemFont(ofSize: 30, weight: .bold)
detailName.backgroundColor = UIColor.white
detailName.textColor = UIColor.black
detailType.textColor = .white
[detailMainImage, detailName, detailType, detailHeart].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
addSubview($0)
}
// give main image a height
// set its Priority to 999 to prevent layout constraint conflict warnings
let mainImageHeightAnchor = detailMainImage.heightAnchor.constraint(equalToConstant: 300.0)
mainImageHeightAnchor.priority = UILayoutPriority(rawValue: 999)
NSLayoutConstraint.activate([
// constrain main image to all 4 sides
detailMainImage.topAnchor.constraint(equalTo: topAnchor),
detailMainImage.leadingAnchor.constraint(equalTo: leadingAnchor),
detailMainImage.trailingAnchor.constraint(equalTo: trailingAnchor),
detailMainImage.bottomAnchor.constraint(equalTo: bottomAnchor),
// activate the height contraint
mainImageHeightAnchor,
// constrain detailType label
// 30-pts from Leading
// 12-pts from Top
detailType.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 30.0),
detailType.topAnchor.constraint(equalTo: topAnchor, constant: 12.0),
// constrain detailName label
// 30-pts from Leading
// 12-pts from Bottom
detailName.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 30.0),
detailName.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12.0),
// constrain detailHeart image
// 12-pts from Trailing
// 12-pts from Bottom
// width: 24 height: equal to width (1:1 square)
detailHeart.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12.0),
detailHeart.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12),
detailHeart.widthAnchor.constraint(equalToConstant: 24),
detailHeart.heightAnchor.constraint(equalTo: detailHeart.widthAnchor),
])
}
}
For this example, I just set the background color of the "heart" image view to red, and I clipped the teacup out of your image:
And this is the result:
Edit - to use the custom view as a Section header view...
Use the same DetailTableHeaderView class from above, but change the table view controller as follows:
class TestSectionHeaderTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 300
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let v = DetailTableHeaderView()
// for example implementation...
if let img = UIImage(named: "teacup") {
v.detailMainImage.image = img
}
v.detailName.text = "Testing the Name"
// for your implementation...
//if let img = UIImage(named: restaurant.image) {
// v.detailMainImage.image = img
//}
//v.detailName.text = restaurant.name
return v
}
return nil;
}
}

UILabel in a custom UIButton that adjusted to the button's size with AutoLayout

I want to have UILabel inside a custom UIButton with constraints to the button size, but to adjust the leading and trailing constraints constants.
The idea is to make the UILabel a bit smaller than the button width (the label takes the font from the button and uses auto shrink).
Adding the relevant code in init with coder of my custom button results with unsatisfied constraints error.
label = UILabel(frame: bounds)
addSubview(label)
translatesAutoresizingMaskIntoConstraints = false
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10.0).isActive = true
label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
label.topAnchor.constraint(equalTo: topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
When I remove the "10.0" constant it works ok, but that idea is to give the label a different size, not the exact size of the button.
Any idea?
Thanks
Instead of additional label and constraints, try to set button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0), and use the regular title of the button.
Instead of regular UIButton, create a subclass of it, and override the intrinsicContentSize method of the button to keep the possibility of autosizing:
class MyButton : UIButton {
open override var intrinsicContentSize: CGSize {
get {
var ics = super.intrinsicContentSize
ics.width = (ics.width < CGFloat(UINT16_MAX)) ? CGFloat(ceil(ics.width + self.titleEdgeInsets.left + self.titleEdgeInsets.right)) : ics.width
ics.height = (ics.height < CGFloat(UINT16_MAX)) ? CGFloat(ceil(ics.height + self.titleEdgeInsets.top + self.titleEdgeInsets.bottom)) : ics.height
return ics
}
}
}
If you need two labels (native button's titleLabel + your label), the approach is the same:
class MyButton : UIButton {
var labelLeading : NSLayoutConstraint!
var labelTrailing : NSLayoutConstraint!
var labelTop : NSLayoutConstraint!
var labelBottom : NSLayoutConstraint!
let label = UILabel(frame: bounds)
public override init(frame: CGRect) {
super.init(frame: frame)
internalInit()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
internalInit()
}
private func internalInit() {
addSubview(label)
/// !!! Important to make label to not translate its autoresizing mask, not the button
label.translatesAutoresizingMaskIntoConstraints = false
labelLeading = label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10.0)
labelTrailing = label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
labelTop = label.topAnchor.constraint(equalTo: topAnchor)
labelBottom = label.bottomAnchor.constraint(equalTo: bottomAnchor)
NSLayoutConstraint.activate([labelLeading, labelTrailing, labelTop, labelBottom)
}
open override var intrinsicContentSize: CGSize {
get {
var ics = super.intrinsicContentSize
ics.width = (ics.width < CGFloat(UINT16_MAX)) ? CGFloat(ceil(ics.width + self.titleEdgeInsets.left + self.titleEdgeInsets.right)) : ics.width
ics.height = (ics.height < CGFloat(UINT16_MAX)) ? CGFloat(ceil(ics.height + self.titleEdgeInsets.top + self.titleEdgeInsets.bottom)) : ics.height
return ics
}
}
}
Found the solution, had to add the translatesAutoresizingMaskIntoConstraints also to the label:
label.translatesAutoresizingMaskIntoConstraints = false

UIView internal constraints

I have created a simple UIView that contains a red box (UIImage) at its centre. When all the constraints are constants the code works fine. However, if I replace the height constraint with one that makes the box half the height of the view then the box disappears.
I assume that this is either because I am doing it wrong (obviously) or I need to do something more to force the constraint to realise the UIView height is greater than zero.
How do I set the redBox height constraint so that it is always half the height of the BoxView?
import UIKit
class BoxView: UIView {
public var redBox: UIImageView
public override init(frame: CGRect) {
redBox = UIImageView(frame: .zero)
redBox.backgroundColor = .red
super.init(frame: frame)
self.backgroundColor = .yellow
addSubview(redBox)
redBox.translatesAutoresizingMaskIntoConstraints = false
let margins = layoutMarginsGuide
redBox.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
redBox.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
redBox.widthAnchor.constraint(equalToConstant: 100).isActive = true
//redBox.heightAnchor.constraint(equalToConstant: 100).isActive = true
redBox.heightAnchor.constraint(equalTo: self.heightAnchor, constant: 0.5)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view = BoxView()
}
}
Replace
redBox.heightAnchor.constraint(equalTo: self.heightAnchor, constant: 0.5)
with
redBox.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.5).isActive = true
NSLayoutConstraint.activate([
redBox.centerXAnchor.constraint(equalTo: self.centerXAnchor),
redBox.centerYAnchor.constraint(equalTo: self.centerYAnchor),
redBox.widthAnchor.constraint(equalToConstant: 100),
redBox.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.5)
])
In your current code first you miss the .isActive = true which has the same effect as if the line doesn't exist , and if specified this will make the box height equal to the view's height + constant ( = 0.5 )
box height = view height * multiplier + constant
and since default multiplier = 1 and you set constant = 0.5 this will be
box height = view height * 1.0 + 0.5
But instead you need
box height = view height * 0.5 + 0 // omit consatnt in constraint and it will be zero
class BoxView: UIView {
public var redBox: UIImageView
public override init(frame: CGRect) {
super.init(frame: frame)
redBox = UIImageView(frame: .zero)
redBox.backgroundColor = .red
self.backgroundColor = .yellow
addSubview(redBox)
redBox.translatesAutoresizingMaskIntoConstraints = false
let margins = layoutMarginsGuide
NSLayoutConstraint.activate([
redBox.centerXAnchor.constraint(equalTo: self.centerXAnchor),
redBox.centerYAnchor.constraint(equalTo: self.centerYAnchor),
redBox.widthAnchor.constraint(equalToConstant: 100),
redBox.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.5)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

Unable to simultaneously satisfy constraints when animating constraint

This is my code:
class AvailableTrainScene: UIView {
let seeMoreButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .blue
v.layer.cornerRadius = 20
v.setTitle("Voir Plus", for: .normal)
return v
}()
var seeMoreBottom: NSLayoutConstraint?
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(seeMoreButton)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
NSLayoutConstraint.activate([
seeMoreButton.heightAnchor.constraint(equalToConstant: 40),
seeMoreButton.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.8),
seeMoreButton.centerXAnchor.constraint(equalTo: centerXAnchor)
])
seeMoreBottom = seeMoreButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
seeMoreBottom?.isActive = false
}
func hideSeeMore(_ hide: Bool) {
if hide {
seeMoreBottom?.constant = 40
}else{
seeMoreBottom?.constant = -8
}
UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 4, options: .curveEaseOut, animations: {
self.layoutIfNeeded()
}, completion: nil)
}
}
i get this error when animating using the function hideSeeMore:
LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x28060dc70 UIButton:0x105534920'Voir Plus'.bottom == DzTrain_2_0.AvailableTrainScene:0x1055300d0.bottom - 8 (active)>",
"<NSLayoutConstraint:0x2806894a0 UIButton:0x105534920'Voir Plus'.bottom == DzTrain_2_0.AvailableTrainScene:0x1055300d0.bottom + 40 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x2806894a0 UIButton:0x105534920'Voir Plus'.bottom == DzTrain_2_0.AvailableTrainScene:0x1055300d0.bottom + 40 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
i tried to add tow constraints one to hide and another to show (priority is deferent) then activate/deactivate them but nothing happend?
Someone have this before?
Put constraints in
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(seeMoreButton)
NSLayoutConstraint.activate([
seeMoreButton.heightAnchor.constraint(equalToConstant: 40),
seeMoreButton.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.8),
seeMoreButton.centerXAnchor.constraint(equalTo: centerXAnchor)
])
seeMoreBottom = seeMoreButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
seeMoreBottom?.isActive = false
}
as this
override func layoutSubviews()
is called for any
self.layoutIfNeeded()
which causes your edit in constant to conflict with a new overwrite here
seeMoreBottom = seeMoreButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
seeMoreBottom?.isActive = false

How to position a subView below a subView with dynamic height?

I have the following UIView:
It is a UIView which contains three subviews. A regular UILabel (Hello World) at the top, a custom UIViewController (CategoryList) which contains a CollectionView with buttons (alpha,beta, ...) and another custom UIViewController with just a label (SALUT).
I do auto-layout programmatically and position SALUT (var sCtrl) below the CategoryList (var catList) with
sCtrl.view.topAnchor.constraint(equalTo: catList.view.bottomAnchor).isActive = true
This results in the picture above, where SALUT is not positioned below the category list as I would like it to be. I sort of understand why since when I set the constraint the buttons are not yet laid out properly in the CollectionView and thus the bottom anchor of the catList is not set.
In the CategoryList:UIVIewController I have the following in order to get the collection view to get the correct height.
override public func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
heightConstraint = collectionView.heightAnchor.constraint(equalToConstant: collectionView.collectionViewLayout.collectionViewContentSize.height)
self.view.addConstraint(heightConstraint)
}
This works but since viewDidLayoutSubviews() is called after the constraint is set on SALUT the SALUT position is wrong. How can I get it right in an easy way? (I guess I could make a controller for my main view and check there when subviews are laid out and then update subview positions but this seems overly complicated to me).
The full code for the main view is below so you can see the layout positioning code. If needed I can also provide the subviews code but I suppose it shouldn't be needed since they should be considered black boxes...
class MyView: UIView {
let label = UILabel()
var sCtrl : SubController
var catList : CategoryList
var topConstraint = NSLayoutConstraint()
override init(frame: CGRect) {
sCtrl = SubController()
catList = CategoryList()
super.init(frame: frame)
setup()
}
private func setup() {
self.backgroundColor = UIColor.green
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.yellow
label.text = "Hello world"
label.textAlignment = .center
self.addSubview(label)
catList.view.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(catList.view)
sCtrl.view.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(sCtrl.view)
let margins = self.layoutMarginsGuide
label.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 0).isActive = true
label.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 0).isActive = true
label.topAnchor.constraint(equalTo: margins.topAnchor, constant: 0).isActive = true
label.heightAnchor.constraint(equalToConstant: 100.0).isActive=true
catList.view.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 0).isActive = true
catList.view.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 1.0).isActive = true
catList.view.widthAnchor.constraint(equalTo: margins.widthAnchor, multiplier: 1.0).isActive = true
catList.view.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant:0).isActive = true
sCtrl.view.topAnchor.constraint(equalTo: catList.view.bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let v = MyView(frame: CGRect(x: 0, y: 0, width: 400, height: 600))
Ok, I found the issue. I had missed to add the height constraint to the CategoryList's own view. Adding this it works just fine.
override public func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
heightConstraint = collectionView.heightAnchor.constraint(equalToConstant: collectionView.collectionViewLayout.collectionViewContentSize.height)
self.view.addConstraint(heightConstraint)
//THE MISSING LINE!!
self.view.heightAnchor.constraint(equalTo: self.collectionView.heightAnchor, constant: 0).isActive = true
}

Resources