I have the following UI definition:
I wanted to define the constraints for the components but whenever I try to add a constraint so that the Bordered Scroll View - Table View.top is topStackView.bottom + 20, I get a constraint conflict. There is a hidden constraint where the Bordered Scroll View - Table View.t must be
topStackView.bottom + 8.
So, my question is where is this +8 constraint coming from? I assume it must be from the NSScrollView that contains the table as all the components inside seem to have Translate Masks Into Constraints.
How can I properly use Auto Layout so achieve what I want?
I haven't done much with MacOS apps, but this may solve your issue...
Delete the Bordered Scroll View - Table View.top is topStackView.bottom + 20 that you're trying to add.
You have them embedded in a vertical stack view - mainStackView - so set the Spacing on that stack view to 20.
It may not look right in Storyboard / IB, but the result should be correct (I have two buttons at the top, but that won't make a difference):
Related
Before when I delete a constraint that sits between two views, the position or size of the two views doesn't get affected(meaning they don't automatically adjust itself in the storyboard). But now it does. An example would be let's we have two UIButtons aligned horizontally. I set the height and width of one equal to the other. Then if I delete one button, the other button will change it's size in the interface builder.
Is there a way to disable the auto adjusting of sizes and position of views when their constraint changes?
I have constraint issues that contradict: Xcode claims that my ScrollView is over-constraint and under-constraint at the same time.
See screenshot:
This looks like an error message that might not be reporting "the missing constraint for: Y position or height" correctly.
The constraint conflict seems valid, because the scroll view is constrained to the top and bottom layout guides which have an inset that will be less than the height of the view controller view. You can get rid of the conflict by removing the scroll View.height = height constraint.
Without seeing your view hierarchy setup, I want to guess that the "Need constraints for: Y position or height" is actually referring to the contents within the scroll view and its scrollable content area. Fully constraining the contents in the scroll view could resolve the missing constraint issue.
I create a XIB in Xcode and add a simple view as a subview:
What I want to achieve is that the subview has a fixed size and the rootview automatically resizes to the size of that subview, leaving a margin of 20.0 around it:
So I add a fixed width and a fixed height constraint to the subview. Then I add the four constraints for the 20.0 margin:
As the superview does not have any constraints there should be neither ambiguity nor conflicting constraints: I would expect the superview to shrink down in order to match the constraints. However, Xcode complains:
These constraints would only be conflicting if the rootview had a fixed size and that appears to be the case. So my question is: How can I make the rootview of a XIB flexible so that it dynamically adjusts its size to match its contents?
(Is that even possible with Interface Builder?)
How can I make the rootview of a XIB flexible so that it dynamically
adjusts its size to match its contents?
Not possible Interface builder.
As the superview does not have any constraints there should be neither
ambiguity nor conflicting constraints
Its not just a super view. Its also an objet in nib. We define simulated size for such views. This could be used to silence the errors. But again these are just simulated.
Is this rootView a view controllers view ? If yes i don't understand why are you trying to fix its withd to 280 and height to 168.
If this view is a custom view that you are going to add to another 'parent' view. Then you should change you simulated size to with 280 and height 168, and when adding this as subview you need to add two more constraints to position this rootview in the 'parent' view.
I had a same issue. I have a view in xib, which had dynamic content and it needed to fit into other superviews. so here is the answer how I achieved that
First you need to
myViewFromXib.translatesAutoresizingMaskIntoConstraints = false to prevent translating autoresizing mask into constraints. Then add subview superViewForMyViewFromXib.addSubview(myViewFromXib). And just add your constraints to superview like this:
NSLayoutConstraint.activate([
(myViewFromXib.topAnchor.constraint(equalTo: superViewForMyViewFromXib.topAnchor, constant: 0))!,
(myViewFromXib.bottomAnchor.constraint(equalTo: superViewForMyViewFromXib.bottomAnchor, constant: 0))!,
(myViewFromXib.leadingAnchor.constraint(equalTo: superViewForMyViewFromXib.leadingAnchor, constant: 0))!,
(myViewFromXib.trailingAnchor.constraint(equalTo: superViewForMyViewFromXib.trailingAnchor, constant: 0))!
])
superViewForMyViewFromXib.setNeedsLayout()
You can do this by editing the xib manually, for example to set a height constraint (in my case it was an inequality to set the minimum height):
add a constraint to a subview for the height
open the xib as a text file
find the constraint you added (eg by Cmd-F'ing to the value of the height)
cut and paste it into the root view's constraint section
Open the xib in interface builder again
The constraint appears and you can edit it like normal.
OMG, I cant believe you accept that this is not possible and change your way , if this was not possible then Xib would be useless. please don't provide wrong info to others your question is well detail but answer is more than poor:
answer is more than easy :
subview.frame.size.height = rootView.frame.size.height
subview.frame.size.width = rootView.frame.size.width
I'm building a OS X application using Swift and Xcode 6.4 on OS X 10.10.5.
On a specific view of my application I would like to have a view like this one Xcode has on the Data Model Editor.
I tried to replicate this view using an OutlineView where each "row" would have a title and a TableView plus two buttons (for the plus and minus buttons). For tests purposes I've separated the title for the TableView+Buttons, something like this (this was one of many different attempts).
Everything is working as expected except the View that has the TableView+Buttons, that is never higher than 17 pixels. If I define everything in one view, I have the same problem. I've tried defining the needed constraints but in that case there is a problem with a constraint that seems automatic called NSView-Encapsulated-Layout-Height, that forces the height to 17 pixels:
NSLayoutConstraint:0x61800008ea10 'NSView-Encapsulated-Layout-Height'
> V:[NotesTable(17)] (Names: NotesTable:0x60000012e2e0 )
I'm not defining any constraint to 17 pixels, I've tried testing with some parameters that usually insert automatic constraints (autoresizesSubviews/translatesAutoresizingMaskIntoConstraints/autoresizingMask) but I was only able to translate that 'special' constraint to another format and the grow doesn't get bigger.
Tried to search the web but I only get cases where that Encapsulated constraint makes sense and is useful.
Do you know where or how can I disable that constraint or change its value to the height I need?
Table and outline views on OS X do not support automatically determining the row height from the dynamic height of the cell views. They either have an explicit static row height, a static row height determined by the design-time height of the cell views, or a dynamic row height determined by the delegate and its implementation of -tableView:heightOfRow: or -outlineView:heightOfRowByItem:.
For your case, you're going to have to implement the delegate method. Furthermore, the delegate method can't query the actual cell view because it may not exist and the outline view would need the row height before creating it. So, the delegate has to compute it some other way.
One way is to keep a standalone view hierarchy of a prototypical cell view. When the delegate is asked for the row height, it configures that view hierarchy as it would be for the actual cell view for that row/item, forces it to lay itself out, and then queries its height. Configuring the view hierarchy may be as simple as setting the objectValue of the top-level view (if it's an NSTableCellView, a control, or otherwise implements the setter). But if your delegate does other configuration, such as in its -outlineView:viewForTableColumn:item: method, then you'll need to replicate that for this prototype view hierarchy.
Also, when any factor that would affect a row's height changes, you have to call the outline view's -noteHeightOfRowsWithIndexesChanged: method to let it know that, so it will re-query your ...heightOfRow... method.
Finally, bare table views are not especially amenable to being constrained to sibling views or their superview. They really want to live in scroll views and continue using springs-and-struts to position and size themselves. See my answer to another question for a discussion of this. It is possible that this has been improved in recent versions of the OS. Anyway, you're going to have to observe the table view's frame-change notifications (and ask it to post such notifications) in order to know when it grows. And your ability to set constraints to relate it to any other views in the cell view hierarchy will be severely limited, because it will need translatesAutoresizingMaskIntoConstraints turned on.
I have a project I am upgrading from Xcode 4.6.3 to Xcode 6.1.1. I opened it in Xcode 6.1.1, and opened each .xib. The format of each .xib changed as expected. I want to attempt to use auto layout. Use Auto Layout is checked and so is Use Size Classes. I changed every appropriate object from Alignment Frame to be Alignment Rectangle.
For a while I couldn’t add any constraints. Then after some trying I could add a few. See the screenshot.
For the selected View, I cannot add a constraint. The Add New Constraints checkboxes and fields are disabled. Only Update Frames is available to be changed. I can’t add an alignment constraint either. Ctrl-drag a line off the view does not add a constraint either. Notice the View height of 411. If I select another object, then select the View again the height will change to be the height of the parent tab bar less. If I keep doing that the height gets smaller and smaller, then Xcode crashes. How can I add a constraint to this view?
The Tab Bar X, Y, Width, and Height are disabled. When I change the Height of the Assigned View Controller View, the Tab Bar Y changes to that value minus the Tab Bar Height of 49. When I select the View, again it’s Height is now 49 less. Select the Tab Bar, it’s Y is now 49 less. And so on. How do I set the dimensions of the Assigned View Controller View that will stick when I cannot add contraints?
I had the same issue. In my case, the view layout setting was set to Translate Mask Into Constraint. I resolved it by changing it to Automatic in Size Inspector.
Xcode 12, Xcode 13
For UI elements where adding constraints are disabled, check Layout in the Size Inspector:
If Layout is set to Autoresizing Mask change to Inferred.
or
Inferred(Constraints) - The UI element already has one or more constraints.
Inferred(Autoresizing Mask) - The UI element currently has no constraints.
Steps to enable autolayouts:
Select any UI element, in storyboard.
Click on Show the Size Inspector.
Click on dropdown beside Layout option, select Automatic from dropdown.
Zev has the answer. You can't add constraints directly to the top-level view in a view controller.
In regards to the height of the View decreasing when selecting it, I started over from the original .xib making small changes and taking notes. When I checked "Use Auto Layout" and "User Size Classes", got alert the document will no longer be compatible with Xcode 5. Window frame size went from 320 568 to 600 600. ibExternalTranslatesAutoresizingMaskIntoConstraints went from 1 to 0. Other changes apparent as well. Adding constraints to objects contained in View one at a time I am getting good results. The behavior of the View height decreasing when I select it is gone.
Trick to enable constraints on the root view:
Inside the xib, drag a new view that will be sibling to the initial root
view.
Move the initial root view inside the sibling view. It will be
able to have own constraints.
Move the initial view back to be a
root.
Delete the empty sibling view.
Xcode 12 Swift 5
Select the UI Control and click on Show Size Inspector window
Change Layout option to Inferred (Autoresizing Mask))
By default the Xcode 12 keep the setting of layout to Autoresizing Mask, If you want to apply the constraint used Inferred