Nazmul Hasan Nazmul Hasan - 5 months ago 35
iOS Question

How to solve Unable to simultaneously satisfy constraints

I am trying with coustom constrain

Menubar class :

import UIKit

class ManuBar: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blueColor()

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")


extension UIView {
func addConstraintsWithFormat(format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerate() {
let key = "v\(index)"
view.translatesAutoresizingMaskIntoConstraints = false
viewsDictionary[key] = view

addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))

View controller :

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.



let menuBar : ManuBar = {
let mb = ManuBar()
return mb

private func setupManuBar(){


view.addConstraintsWithFormat("H:|[v0]|",views : menuBar)
view.addConstraintsWithFormat("V:|-16-[v0(40)]|",views : menuBar)


enter image description here

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:0x7ff891d27c70 V:|-(16)-[Tab_Menu_Bar_Programmatically.ManuBar:0x7ff891d19f30] (Names: '|':UIView:0x7ff891d1b570 )>",
"<NSLayoutConstraint:0x7ff891d27f40 V:[Tab_Menu_Bar_Programmatically.ManuBar:0x7ff891d19f30(40)]>",
"<NSLayoutConstraint:0x7ff891d0fc50 V:[Tab_Menu_Bar_Programmatically.ManuBar:0x7ff891d19f30]-(0)-| (Names: '|':UIView:0x7ff891d1b570 )>",
"<NSLayoutConstraint:0x7ff891d1c8e0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7ff891d1b570(736)]>"

Will attempt to recover by breaking constraint

if your need more information let me know


You forgot view in view.translatesAutoresizingMaskIntoConstraints = false, thus you are setting the translatesAutoresizingMaskIntoConstraints on the cell and not on the individual views.

Change the for loop to

for (index, view) in views.enumerate(){
                let key = "v\(index)"
                view.translatesAutoresizingMaskIntoConstraints = false
                viewsDictionry[key] = view


I would also look at lazy loading the views by adding the lazy keyword.

Update :

You significantly changed your code though. I tested on your first example, and it worked. Your problem now is: view.addConstraintsWithFormat("V:|-16-[v0(40)]|",views : menuBar) Your are basically setting the height of the menubar twice. You are setting it to a fixed height of 40, but you are also telling it to stretch according to superview. 16 pixels top space + 0 pixels at bottom. Remove the | at the end

like this way:

 view.addConstraintsWithFormat("H:|[v0]|",views : menuBar) 
 view.addConstraintsWithFormat("V:|-16-[v0(40)]",views : menuBar)