Autolayout warning when using prompt in navigation bar - ios

I am using a navigation bar with a prompt, looking like this:
I receive the following auto layout warnings in the console:
2019-09-01 21:26:03.225576+0200 Hortus[85622:2386450] [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:0x600002d16990 UILabel:0x7fc9eb816f50'Testen'.centerX == _UINavigationBarModernPromptView:0x7fc9ebe27780.centerX (active)>",
"<NSLayoutConstraint:0x600002d15c20 UILabel:0x7fc9eb816f50'Testen'.leading >= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x600002d10ff0 '_UITemporaryLayoutWidth' _UINavigationBarModernPromptView:0x7fc9ebe27780.width == 0 (active)>",
"<NSLayoutConstraint:0x600002d16a30 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarModernPromptView:0x7fc9ebe27780 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002d15c20 UILabel:0x7fc9eb816f50'Testen'.leading >= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.leading (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.
2019-09-01 21:26:03.226198+0200 Hortus[85622:2386450] [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:0x600002d16990 UILabel:0x7fc9eb816f50'Testen'.centerX == _UINavigationBarModernPromptView:0x7fc9ebe27780.centerX (active)>",
"<NSLayoutConstraint:0x600002d15540 UILabel:0x7fc9eb816f50'Testen'.trailing <= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x600002d10ff0 '_UITemporaryLayoutWidth' _UINavigationBarModernPromptView:0x7fc9ebe27780.width == 0 (active)>",
"<NSLayoutConstraint:0x600002d14320 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide']-(8)-|(LTR) (active, names: '|':_UINavigationBarModernPromptView:0x7fc9ebe27780 )>"
"<NSAutoresizingMaskLayoutConstraint:0x60000043be30 h=--& v=--& _UINavigationBarModernPromptView:0x7fb3c6416640.width == 0 (active)>",
"<NSLayoutConstraint:0x600000421400 UILabel:0x7fb3c66d8bf0'Plant guide'.centerX == _UINavigationBarModernPromptView:0x7fb3c6416640.centerX (active)>",
"<NSLayoutConstraint:0x600000421f90 UILabel:0x7fb3c66d8bf0'Plant guide'.trailing <= UILayoutGuide:0x600001e58fc0'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x600000421e00 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001e58fc0'UIViewLayoutMarginsGuide']-(20)-|(LTR) (active, names: '|':_UINavigationBarModernPromptView:0x7fb3c6416640 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002d15540 UILabel:0x7fc9eb816f50'Testen'.trailing <= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.trailing (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.
These warnings are given for the prompt in the navigation bar, but I do not set the autolayout constraints for the navigation bar. This should be handled by IOS itself.
What can I do to fix these warnings?
[Edit] The warnings are given in prepare() when segueing to the view controller. I set the prompt in prepare(). The segue is to a storyboard reference and is defined as 'present modally'. The storyboard reference references a UINavigationController. The prompt is set on the root view controller of the navigation controller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
switch identifier {
case "editImage":
let nc = segue.destination as? TTImageNavigationController
if let rvc = nc?.rootViewController as? TTImageEditorController {
rvc.navigationItem.prompt = navigationItem.prompt
}
default:
break
}
}
}
[EDIT] It seems that the problem is not clear to most of the people who react. Again... I do not set any auto layout settings in code or storyboard. The problem occurs in the navigation bar after setting the prompt in the prepare() function prior to the segue. The code is given above. Any reactions that say that I have to change or check my constraints are not helpful, because there are no constraints to check!
[EDIT] As requested by Mohsen, I created a small sample project on github: https://bitbucket.org/Leontien/navigationbarautolayoutwarning/src/master/

It’s not your bug. It’s Apple’s bug. Ignore the console. File a report with Apple and move on.

Set priority to your constraints. This will fulfill constraints according to priority. Please refer this link.

you set centerX for the label and set leading and trailing for right and left buttons, also you didn't set any width constraint for UILabel
"<NSLayoutConstraint:0x600002d16990 UILabel:0x7fc9eb816f50'Testen'.centerX == _UINavigationBarModernPromptView:0x7fc9ebe27780.centerX (active)>",
"<NSLayoutConstraint:0x600002d15c20 UILabel:0x7fc9eb816f50'Testen'.leading >= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x600002d10ff0 '_UITemporaryLayoutWidth' _UINavigationBarModernPromptView:0x7fc9ebe27780.width == 0 (active)>",
"<NSLayoutConstraint:0x600002d16a30 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarModernPromptView:0x7fc9ebe27780 )>"
.
"<NSLayoutConstraint:0x600002d16990 UILabel:0x7fc9eb816f50'Testen'.centerX == _UINavigationBarModernPromptView:0x7fc9ebe27780.centerX (active)>",
"<NSLayoutConstraint:0x600002d15540 UILabel:0x7fc9eb816f50'Testen'.trailing <= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x600002d10ff0 '_UITemporaryLayoutWidth' _UINavigationBarModernPromptView:0x7fc9ebe27780.width == 0 (active)>",
"<NSLayoutConstraint:0x600002d14320 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide']-(8)-|(LTR) (active, names: '|':_UINavigationBarModernPromptView:0x7fc9ebe27780 )>"
"<NSAutoresizingMaskLayoutConstraint:0x60000043be30 h=--& v=--& _UINavigationBarModernPromptView:0x7fb3c6416640.width == 0 (active)>",
"<NSLayoutConstraint:0x600000421400 UILabel:0x7fb3c66d8bf0'Plant guide'.centerX == _UINavigationBarModernPromptView:0x7fb3c6416640.centerX (active)>",
"<NSLayoutConstraint:0x600000421f90 UILabel:0x7fb3c66d8bf0'Plant guide'.trailing <= UILayoutGuide:0x600001e58fc0'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x600000421e00 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001e58fc0'UIViewLayoutMarginsGuide']-(20)-|(LTR) (active, names: '|':_UINavigationBarModernPromptView:0x7fb3c6416640 )>"
for solve this warning you should remove leading and trailing constraint from UILabel and set left button with leading to superview and right button's trailing to superview as well.
or you can use a navigation bar object, it has a label and also you can add barButtonItem to it.
you can add width constraint for the UILabel but it will be conflict on another devices when you run it on some other devices

This helps to debug broken constraint issue by providing a more readable output
Create an extension for NSLayoutConstraint
extension NSLayoutConstraint {
override open var description: String {
let id = identifier ?? ""
return "Constraint ID: \(id), constant: \(constant)"
}
}
set a unique identifier for every constraint in the storyboard
you'll get a more readable output pointing out which constraint id is broken
change the priority for broken constraint

Related

DatePicker with graphical style breaks layout constraints on iOS 16.0

The following code breaks the layout constraints when running on Xcode-14.0.0-Beta 5 with iOS 16.0
struct ContentView: View {
#State var date = Date()
var body: some View {
DatePicker(selection: $date, displayedComponents: .date, label: { EmptyView() })
.datePickerStyle(.graphical)
}
}
Any suggestions how to fix or hide the warning?
Console warning:
2022-08-24 17:16:40.471325+0300 DatePickerProblem[77364:483284] [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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003559180 h=--& v=--& _UIDatePickerCalendarTimeView:0x7fe15c322520.height == 0 (active)>",
"<NSLayoutConstraint:0x60000352bca0 _UIDatePickerCompactTimeLabel:0x7fe15c322bc0.centerY == _UIDatePickerCalendarTimeView:0x7fe15c322520.centerY - 1 (active)>",
"<NSLayoutConstraint:0x60000352bcf0 V:|-(>=0)-[_UIDatePickerCompactTimeLabel:0x7fe15c322bc0] (active, names: '|':_UIDatePickerCalendarTimeView:0x7fe15c322520 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000352bca0 _UIDatePickerCompactTimeLabel:0x7fe15c322bc0.centerY == _UIDatePickerCalendarTimeView:0x7fe15c322520.centerY - 1 (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.
2022-08-24 17:16:40.471971+0300 DatePickerProblem[77364:483284] [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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003559180 h=--& v=--& _UIDatePickerCalendarTimeView:0x7fe15c322520.height == 0 (active)>",
"<NSLayoutConstraint:0x60000352bd40 UILabel:0x7fe15c324d00.centerY == _UIDatePickerCalendarTimeView:0x7fe15c322520.centerY - 1 (active)>",
"<NSLayoutConstraint:0x60000352bd90 V:|-(>=0)-[UILabel:0x7fe15c324d00] (active, names: '|':_UIDatePickerCalendarTimeView:0x7fe15c322520 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000352bd40 UILabel:0x7fe15c324d00.centerY == _UIDatePickerCalendarTimeView:0x7fe15c322520.centerY - 1 (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.
2022-08-24 17:16:40.478778+0300 DatePickerProblem[77364:483284] [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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<_UISystemBaselineConstraint:0x60000355c190 H:[UILabel:0x7fe15c324d00]-(>=NSLayoutAnchorConstraintSpace(8))-[_UIDatePickerCompactTimeLabel:0x7fe15c322bc0] (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x6000035590e0 h=--& v=--& _UIDatePickerCalendarTimeView:0x7fe15c322520.width == 0 (active)>",
"<NSLayoutConstraint:0x60000352bf20 UILabel:0x7fe15c324d00.leading == UILayoutGuide:0x600002f06760'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x60000355c230 _UIDatePickerCompactTimeLabel:0x7fe15c322bc0.trailing == UILayoutGuide:0x600002f06760'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x60000352be30 'UIView-leftMargin-guide-constraint' H:|-(8)-UILayoutGuide:0x600002f06760'UIViewLayoutMarginsGuide' (active, names: '|':_UIDatePickerCalendarTimeView:0x7fe15c322520 )>",
"<NSLayoutConstraint:0x60000352bed0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600002f06760'UIViewLayoutMarginsGuide']-(8)-|(LTR) (active, names: '|':_UIDatePickerCalendarTimeView:0x7fe15c322520 )>"
)
Will attempt to recover by breaking constraint
<_UISystemBaselineConstraint:0x60000355c190 H:[UILabel:0x7fe15c324d00]-(>=NSLayoutAnchorConstraintSpace(8))-[_UIDatePickerCompactTimeLabel:0x7fe15c322bc0] (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.
2022-08-24 17:16:40.512015+0300 DatePickerProblem[77364:483284] [UICalendarView] UICalendarView's height is smaller than it can render its content in; defaulting to the minimum height.
2022-08-24 17:16:40.548888+0300 DatePickerProblem[77364:483284] [UICalendarView] UICalendarView's height is smaller than it can render its content in; defaulting to the minimum height.
It seems that the new underlying UICalendarView has some AutoLayout Issues when used by its SwiftUI DatePicker wrapper.
You can see this even better, when you enable the Time Component too (displayedComponents: [.date, .hourAndMinute]). This gives a much longer Auto-Layout warning about the time label constraint being broken.
For me this happens especially when the SwiftUI View is embedded using an UIHostingController which is pushed onto a Navigation Stack.
In my case, I was able to fix it by giving the DatePicker its default width as its frame width:
struct ContentView: View {
#State var date = Date()
var body: some View {
DatePicker(
selection: $date,
displayedComponents: [.date, .hourAndMinute],
label: { EmptyView() }
)
.datePickerStyle(.graphical)
.frame(width: 320) // Bugfix for AutoLayout-Issue
}
}

SwiftUI LayoutConstraints NavigationView navigationViewStyle

I've got a SwiftUI View containing a NavigationView and some NavigationLinks. For some reason I got a error message regarding LayoutConstraints. This error is also discussed in this SO Question, with recommended solution setting the navigationViewStyle by .navigationViewStyle(StackNavigationViewStyle()) or .navigationViewStyle(.stack).
However by setting the navigationViewStyle to StackNavigation, the Layout is modified in a non intended way. Using StackNavigation will change the Layout on both IOS and IPadOS to a simple List. Not configuring navigationViewStyle or setting it to .automatic will lead to having a List on IOS and a multiple columns on IPadOS.
Additionally the LayoutConstraints error seems to be a error only occur on IOS.
How can I get rid of the LayoutConstraints error (on IOS) without using StackNavigation? Is it possible to distinguish between IOS/IPadOS?
My View:
struct ProgramView: View {
#ObservedObject var itemViewModel: ItemViewModel
var body: some View {
NavigationView {
VStack {
ForEach(itemViewModel.filteredSessions, id: \.id) { session in
NavigationLink(
destination: SessionDetail(session: session)
) {
SessionRow(session: session)
}
}
// shortened for brevity
}
.navigationTitle("Program")
.navigationBarItems(trailing: UpdateApiButton(itemViewModel: itemViewModel))
}
.navigationViewStyle(.automatic) // default behaviour
.navigationViewStyle(.stack) // Stack; shows a List for IOS+IPadOS; IPadOS should remain multi-column
.navigationViewStyle(.columns) // Uses multi-column also in IOS
}
}
The LayoutConstraints error:
2022-04-06 20:43:49.474918+0200 XXX[1505:41050] [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:0x282b8b2f0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x104c1f610]-(6)-[_UIModernBarButton:0x104c1d7e0'Program'] (active)>",
"<NSLayoutConstraint:0x282b8b340 'CB_Trailing_Trailing' _UIModernBarButton:0x104c1d7e0'Program'.trailing <= _UIButtonBarButton:0x104c1d400.trailing (active)>",
"<NSLayoutConstraint:0x282b813b0 'UINav_static_button_horiz_position' _UIModernBarButton:0x104c1f610.leading == UILayoutGuide:0x2831b9a40'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x282b8b7a0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x104c1d400]-(6)-[UILayoutGuide:0x2831b9960'UINavigationBarItemContentLayoutGuide'] (active)>",
"<NSLayoutConstraint:0x282b89680 'UINavItemContentGuide-trailing' UILayoutGuide:0x2831b9960'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x104c1c430.trailing (active)>",
"<NSLayoutConstraint:0x282bfce60 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x104c1c430.width == 0 (active)>",
"<NSLayoutConstraint:0x282b89a40 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x2831b9a40'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarContentView:0x104c1c430 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x282b8b2f0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x104c1f610]-(6)-[_UIModernBarButton:0x104c1d7e0'Program'] (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.
2022-04-06 20:43:49.476126+0200 XXX[1505:41050] [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:0x282b8a760 UIView:0x104c20b90.trailing == _UIBackButtonMaskView:0x104c1afe0.trailing (active)>",
"<NSLayoutConstraint:0x282b80eb0 'Mask_Trailing_Trailing' _UIBackButtonMaskView:0x104c1afe0.trailing == _UIButtonBarButton:0x104c1d400.trailing (active)>",
"<NSLayoutConstraint:0x282b80ff0 'MaskEV_Leading_BIB_Trailing' H:[_UIModernBarButton:0x104c1f610]-(0)-[UIView:0x104c20b90] (active)>",
"<NSLayoutConstraint:0x282b813b0 'UINav_static_button_horiz_position' _UIModernBarButton:0x104c1f610.leading == UILayoutGuide:0x2831b9a40'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x282b8b7a0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x104c1d400]-(6)-[UILayoutGuide:0x2831b9960'UINavigationBarItemContentLayoutGuide'] (active)>",
"<NSLayoutConstraint:0x282b89680 'UINavItemContentGuide-trailing' UILayoutGuide:0x2831b9960'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x104c1c430.trailing (active)>",
"<NSLayoutConstraint:0x282bfce60 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x104c1c430.width == 0 (active)>",
"<NSLayoutConstraint:0x282b89a40 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x2831b9a40'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarContentView:0x104c1c430 )>"
)
Thanks in advance!

iOS UITableViewCell with UIContextMenuConfiguration: Unable to simultaneously satisfy constraints

I followed this tutorial of raywenderlich.com to display a context menu when long pressing on a UITableViewCell and came up with the following simplified version to test the implementation:
import UIKit
class ViewController: UITableViewController {
override func tableView(
_ tableView: UITableView,
contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint)
-> UIContextMenuConfiguration? {
let identifier = "\(indexPath.row)" as NSString
return UIContextMenuConfiguration(
identifier: identifier,
previewProvider: nil) { _ in
let shareAction = UIAction(
title: "Share",
image: UIImage(systemName: "square.and.arrow.up")) { _ in
print("share")
}
return UIMenu(title: "", image: nil, children: [shareAction])
}
}
}
The ViewController is a plain UITableViewController with Static Cells in the Storyboard. When I run the application and long press the table cell then the code works as expected, but I get the following AutoLayout errors in the console and I do not understand why?
2020-07-06 15:22:36.879780+0200 Test[1033:453998] [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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x282308780 h=--& v=--& UIInterfaceActionGroupView:0x102833490.width == 0 (active)>",
"<NSLayoutConstraint:0x28231bca0 UILabel:0x102921c50'Share Item'.leading == UILayoutGuide:0x283939420'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x28231bcf0 UILabel:0x102921c50'Share Item'.trailing == UILayoutGuide:0x283939420'UIViewLayoutMarginsGuide'.trailing (active)>",
"<NSLayoutConstraint:0x282315db0 _UIInterfaceActionGroupHeaderScrollView:0x103063600.width == _UIPreviewPlatterActionsTitleView:0x102921590'Share Item'.width (active)>",
"<NSLayoutConstraint:0x28231a800 H:|-(0)-[_UIContentConstraintsLayoutGuide:0x1028336f0''] (active, names: '|':UIInterfaceActionGroupView:0x102833490 )>",
"<NSLayoutConstraint:0x28231a850 _UIContentConstraintsLayoutGuide:0x1028336f0''.trailing == UIInterfaceActionGroupView:0x102833490.trailing (active)>",
"<NSLayoutConstraint:0x28232fd40 _UIInterfaceActionGroupHeaderScrollView:0x103063600.width == _UIContentConstraintsLayoutGuide:0x1028336f0''.width (active)>",
"<NSLayoutConstraint:0x28231cfa0 'UIView-leftMargin-guide-constraint' H:|-(12)-[UILayoutGuide:0x283939420'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UIPreviewPlatterActionsTitleView:0x102921590'Share Item' )>",
"<NSLayoutConstraint:0x28231bc50 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x283939420'UIViewLayoutMarginsGuide']-(12)-|(LTR) (active, names: '|':_UIPreviewPlatterActionsTitleView:0x102921590'Share Item' )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x28231bcf0 UILabel:0x102921c50'Share Item'.trailing == UILayoutGuide:0x283939420'UIViewLayoutMarginsGuide'.trailing (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.
2020-07-06 15:22:36.881528+0200 Test[1033:453998] [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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x2823088c0 h=--& v=--& UIInterfaceActionGroupView:0x102833490.height == 0 (active)>",
"<NSLayoutConstraint:0x28231b070 groupView.actionsSequence....height >= 44 (active, names: groupView.actionsSequence...:0x103062e00 )>",
"<NSLayoutConstraint:0x2823135c0 V:[_UIInterfaceActionGroupHeaderScrollView:0x103063600]-(0)-[_UIInterfaceActionVibrantSeparatorView:0x10283ab40] (active)>",
"<NSLayoutConstraint:0x2823140a0 V:[_UIInterfaceActionVibrantSeparatorView:0x10283ab40]-(0)-[groupView.actionsSequence...] (active, names: groupView.actionsSequence...:0x103062e00 )>",
"<NSLayoutConstraint:0x28231ada0 UIInterfaceActionGroupView:0x102833490.top == _UIContentConstraintsLayoutGuide:0x1028336f0''.top (active)>",
"<NSLayoutConstraint:0x28231adf0 V:[_UIContentConstraintsLayoutGuide:0x1028336f0'']-(0)-| (active, names: '|':UIInterfaceActionGroupView:0x102833490 )>",
"<NSLayoutConstraint:0x282314190 _UIInterfaceActionGroupHeaderScrollView:0x103063600.top == _UIContentConstraintsLayoutGuide:0x1028336f0''.top (active)>",
"<NSLayoutConstraint:0x2823141e0 groupView.actionsSequence....bottom == _UIContentConstraintsLayoutGuide:0x1028336f0''.bottom (active, names: groupView.actionsSequence...:0x103062e00 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x28231b070 groupView.actionsSequence....height >= 44 (active, names: groupView.actionsSequence...:0x103062e00 )>
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.

Scrollview working by breaking constraint

I have a UIViewController containing a scrollView: the scrollView is pinned (0,0,0,0) to the superview.
The scrollView contains a Content View with the following constraints set:
Inside this view, I put a square image view: it is pinned this way and its height equals the width of its superview.
Anything I described until now is done manually in the storyboard.
Then, using a simple button (so, after viewDidLoad, viewDidLayoutSubviews, etc.), I execute the following function (that 375 is currently hard-coded, but it is the heigth of the image view frame on the device I’m using for testing purposes):
var startH = 0
for i in 0...10 {
let v = UIView(frame: CGRect(x: 0, y: 375+startH, width: 375, height: startH))
v.backgroundColor = .random
contentView.addSubview(v)
startH += 100
}
let scrollerLayoutGuide = scrollView.contentLayoutGuide
scrollerLayoutGuide.heightAnchor.constraint(equalToConstant: CGFloat(startH+375)).isActive = true
scrollView.contentSize = contentView.frame.size
This should (and actually it does) create 11 views under the imageView, inside the contentView of the scrollView.
Problem is when I execute this function I get the following error:
[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:0x6000019d7d40 UIView:0x7fe66940aa30.height == UIScrollView:0x7fe66985b400.height (active)>",
"<NSLayoutConstraint:0x6000019d7d90 V:|-(0)-[UIView:0x7fe66940aa30] (active, names: '|':UIScrollView:0x7fe66985b400 )>",
"<NSLayoutConstraint:0x6000019d7de0 V:[UIView:0x7fe66940aa30]-(0)-| (active, names: '|':UIScrollView:0x7fe66985b400 )>",
"<NSLayoutConstraint:0x6000019e4690 UILayoutGuide:0x6000003c81c0'UIViewSafeAreaLayoutGuide'.bottom == UIScrollView:0x7fe66985b400.bottom (active)>",
"<NSLayoutConstraint:0x6000019e4730 UIScrollView:0x7fe66985b400.top == UILayoutGuide:0x6000003c81c0'UIViewSafeAreaLayoutGuide'.top (active)>",
"<NSLayoutConstraint:0x6000019e11d0 _UIScrollViewLayoutGuide:0x6000003ac1c0'UIScrollView-contentLayoutGuide'.height
== 1475 (active)>",
"<NSLayoutConstraint:0x6000019e9540 'UIView-Encapsulated-Layout-Height' UIView:0x7fe66950bdb0.height == 667 (active)>",
"<NSLayoutConstraint:0x6000019e45f0 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x6000003c81c0'UIViewSafeAreaLayoutGuide']-(0)-| (active, names: '|':UIView:0x7fe66950bdb0 )>",
"<NSLayoutConstraint:0x6000019e4550 'UIViewSafeAreaLayoutGuide-top' V:|-(20)-[UILayoutGuide:0x6000003c81c0'UIViewSafeAreaLayoutGuide'] (active, names: '|':UIView:0x7fe66950bdb0 )>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6000019d7de0 V:[UIView:0x7fe66940aa30]-(0)-| (active, names: '|':UIScrollView:0x7fe66985b400 )>
Problem is I can't understand what constraint is causing this issue and, most of all, why. Could you help me?
There is conflict between your constraints with contentView
1. There is Top, bottom, leading, trailing with the scrollview
2. Fixed height and fixed width constraint of contentView
These two will conflict each other as OS will not be sure which constraint to fulfil.
As a solution reduce the priority of height and width constraint so that the more priority is given to the constraint with respect to the scrollView.
For more details please follow below tutorial
How to configure a UIScrollView with Auto Layout

How to add UITextField programmatically to UIScrollView without breaking constraints?

The case goes like this: I have a form embed in a UIScrollView, in the form there's a part where the user can add N numbers of textField, so if at first I had this:
UITextField - textField1
UIButton - Add
When Add is pressed I want to have this:
UITextField - textField1
UITextField - textField2
UIButton - Add
And although the text field is added, the constraints cannot be satisfied and therefore the UI breaks
The log for that:
[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.
(
"<_UILayoutSupportConstraint:0x1c4c800a0 _UILayoutGuide:0x106ccf4e0.height == 64 (active)>",
"<_UILayoutSupportConstraint:0x1c4c80050 V:|-(0)-[_UILayoutGuide:0x106ccf4e0] (active, names: '|':UIView:0x106ccdc10 )>",
"<_UILayoutSupportConstraint:0x1c4c80ff0 _UILayoutGuide:0x106ccf6d0.height == 0 (active)>",
"<_UILayoutSupportConstraint:0x1c4c801e0 _UILayoutGuide:0x106ccf6d0.bottom == UIView:0x106ccdc10.bottom (active)>",
"<NSLayoutConstraint:0x1c4c80550 UIImageView:0x106cce6c0.height == 5 (active)>",
"<NSLayoutConstraint:0x1c4c80640 UILabel:0x106ccf070'Completa tu Perfil'.height == 24 (active)>",
"<NSLayoutConstraint:0x1c0c886b0 form-view.height == 579 (active, names: form-view:0x10ed307c0 )>",
"<NSLayoutConstraint:0x1c0c8a1e0 V:|-(0)-[form-view] (active, names: form-view:0x10ed307c0, '|':UIScrollView:0x107a37000 )>",
"<NSLayoutConstraint:0x1c0c8a320 V:[form-view]-(0)-[profiles-view] (active, names: profiles-view:0x106ccd430, form-view:0x10ed307c0 )>",
"<NSLayoutConstraint:0x1c4c80af0 V:[_UILayoutGuide:0x106ccf4e0]-(10)-[UILabel:0x106ccf070'Completa tu Perfil'] (active)>",
"<NSLayoutConstraint:0x1c4c80cd0 V:[UILabel:0x106ccf070'Completa tu Perfil']-(8)-[UIImageView:0x106cce6c0] (active)>",
"<NSLayoutConstraint:0x1c4c80d20 V:[UIImageView:0x106cce6c0]-(0)-[UIScrollView:0x107a37000] (active)>",
"<NSLayoutConstraint:0x1c4c80e10 UIImageView:0x106ccddf0.top == profiles-view.top (active, names: profiles-view:0x106ccd430 )>",
"<NSLayoutConstraint:0x1c4c80f00 V:[UIImageView:0x106ccddf0]-(0)-[_UILayoutGuide:0x106ccf6d0] (active)>",
"<NSLayoutConstraint:0x1c0c8bdb0 'UIView-Encapsulated-Layout-Height' UIView:0x106ccdc10.height == 667 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1c0c886b0 form-view.height == 579 (active, names: form-view:0x10ed307c0 )>
So, since the height constraint was broken, AutoLayout arrange the elements following the margin constraints which allows the views broke that way.
That being said, the way I am adding the constraints is this:
func addModelButtonAction(_ sender: Any) { // an IBAction
addButton.isHighlighted = false
let last = modelTextFields.last!
let textField = last.createCopy() // extension method for creating a copy of the last textField
modelTextFieldsView.insertSubview(textField, belowSubview: last)
let growth = last.frame.height + 8 // the 8 represents the top spacing between textFields
formViewHeight.constant += growth // increasing height of the view where the form is embeded
modelTextFieldsHeight.constant += growth // increasing height of the view where the textFields are embeded
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded()
textField.frame.origin.y += growth // moving new textField below the previous one
}
modelTextFields.append(textField) // array of UITextField to keep track of the textFields in screen
}
And the view's alignment rectangles:
And the view's hierarchy:
What can I do to make this work as expected? I appreciate your help in this, because I've been trying to figure this out without any success.
Thanks in advance!
You have a couple of options to solve this layout issue:
Personally, I might try embedding these views in a UIStackView. The stack view will work to manage the vertical layout for you, allowing you to insert and remove subviews as needed. UIStackView is especially easy to work with from Interface Builder if you use Storyboards.
Alternatively, you could use a UITableView for this layout; it would manage the vertical flow on your behalf, abstracting much of the layout headache from you, though you'd need to manage the data source and cells yourself.
You could probably get this layout to work without either of these by carefully managing the priority of different constraints, allowing some to be broken as the layout changes, but this can be a tedious chore.

Resources