Problems with NSTabView autolayout (view based NSTableViews inside tabs) - cocoa

I have an NSTabView which has 5 tabs. Each tab contains an NSTableView (which, as default, is nested in NSScrollView). This is all loaded from a xib file with autolayout turned on. I'd like each table to fully occupy it's respective tab. Using autolayout I select each tab and carefully setup the NSScrollView so that it snaps its top, bottom, lead, and trailing edges to superview. After I do this for the 5 tabs, I'll resize the xib to give it a test. When I cycle back through the tabs, some stay snapped to the superview and some do not. There appears to be some sort of order to this but it is beyond what I can explain.
What's really frustrating is that sometime this will run okay, and sometimes it will crash because of unsatifyable constraints. For now I am using springs/struts but I like autolayout and would like to get it working.
I am afraid that it is the nesting of the tables inside scroll views which is causing the issues.
If you lay this out your own nib, you should be able to reproduce it.
I am hopeful that this can be solved by adding NSLayoutConstraints at runtime, if OSX is anything like iOS's autolayout. Using IB you cannot assing constraints to anythign other than super and sibling (no cousins). This can be done at runtime.
I am hoping someone out there has encountered this issue and has a solution.

Why do the constraints in the layout show a fixed constant instead of auto? Could that be it?
You mentioned cousin constraints, I believe you can add in the in IB if you select two views in the document outline on the left hand side and then add a constraint. I'm unable to pick any two views in IB if I'm using the main window but I can do it in the document outline.
Lastly, you mention it will run okay sometimes but not others - and if that's the case it sounds like something else is adding constraints. Instruments should have a template for tracing constraints, if you can reproduce it you may get a clue to what is introducing the incompatible constraint.

Related

Why does this simple autolayout collapse in IB?

I have a window with a textview in a scrollview and two buttons. I have added constraints at all sides, leading, trailing, top, and bottom. For the buttons I have fixed width and height, and distance to bottom and edge.
I want it to look something like this:
And this is what Interface Builder keeps giving me:
It also keeps offering to fix ambiguities by adding missing constraints, but actually clicking the button has no effect. No constraints are added. The error (and the offer to fix it) remains.
I've tried added the constraints it is asking for, although I can't see why they would be needed and at least the buttons already have (fixed) width constraints. But I keep getting errors and the window keeps getting shrunk to nothing.
When actually running the program, the window looks fine. But I suspect these autolayout errors have something to do with this problem: NSTextView in magnified NSScrollView breaks on resize
The project is here: https://github.com/angstsmurf/spatterlight/tree/helpviewtest
(The branch helpviewtest is a cut down test case created specifically for this problem.)
You have a Text view embedded in a Clip view embedded in a Scroll view, none of which have an intrinsic content size.
IB has no idea what's going to go on at run-time.
So, while everything looks great when you run this, your code will have supplied enough information to lay things out as desired.
To get it to "look right" in IB (and avoid the errors/warnings), you can give the ScrollView a Placeholder Intrinsic Content Size:

Xcode Interface Builder 9.1 Collection View Cells have too small "expected height"

Recently, I encountered a problem with the Interface Builder which I failed to solve. I'm using a UICollectionViewController and whenever I add a Cell it immediately shows a orange rectangle (indicating a auto-layout update). Updating the frame/cell doesn't change anything. The warning associated with the orange marking reads "Expected: height=Y, Actual: height=X" where Y is always exactly X-100. That is: When I set a custom cell height of 193, it says "Expected: height=93". This happens no matter which size I enter.
The problem with this is that I have many difficulties with the subviews and their constraints. For instance, if I add a subview with the constraint to keep the same height as the cell, it will be shown as 93 (the expected height), even though my cell should be 193. Layouting becomes impossible.
I tried adding a new cell, same issue. I tried removing every subview and re-adding them, nothing.
Any ideas are highly welcome.
Thank you very much!
Solution found ! (Xcode 9.2)
The bug seems to only happen when you try to embed the UICollectionViewController in a Container View in the storyboard. The workaround is to remove the segue while you set your constraints, and re-add the segue once your constraints are well set.
I also think that setting UICollectionViewController size as Freeform could be responsible of the bug. If so, try to set constraints before changing the size.
Hope this helps !
I'm not sure if this solved it, but the warning and issue disappeared once I did the following:
Select the CollectionViewController in Interface Builder and set the size to freeform and make the height larger, large enough to show all your template cells. (Plus: maybe refresh the views). That solved the issue for me.
In my case, I have added two collection view cell, withing collection view Controller.Only setting the view controller to free form wont help us. Need to update the size, to get rid of the warning.
updating collection view cell frames, wont help us. Warning will be continuously changing its length.
I tried to work without considering the warning, as I have set my collection view cell size programmatically and it does working as expected.
I think it's just a strange bug
I delete the collectionViewController in SB, then create a new one, the problem disappear
This is a common bug with Interface Builder. I don't know the exact cause, but I believe it's something to do with placing collection views inside arrangement views that do not have an exact size. I've seen it when putting them in scroll views, stack views and container views.
There is a simple fix, although I believe this to be a bug in IB that ought to be corrected by Apple rather than hacking around it. The solution (for me at least - your mileage may vary!) is to place the collection view inside an ordinary UIView and pin its edges to it, then place that parent view in the position that you wanted your collection view with all the constraints it required.
I usually found that there were actually no auto layout issues at run-time, which is why I believe this to be purely an Interface Builder bug.

Auto Resizing in Xcode 8

As I know, before XCode 7 appear, we have Auto Resizing go with Auto Layout (appear in XCode 5), but when XCode 7 appear, Auto Resizing be removed and this appeared again in XCode 8. My question are:
Why Apple put back this feature in XCode 8? Do this feature have
some new functions than previous?
We can use Auto Resizing with Auto Layout in the same page. What are
the benefits of using parallelism like that?
I searched in Apple document but not find info about Auto Resizing
Supply any document link or keyword will be appreciated. Thanks
Autoresizing wasn't removed previously, when autolayout was introduced. But in Interface Builder it was available only at the level of the individual xib or storyboard file: such a file either used exclusively autoresizing or exclusively autolayout. Your views configured in code could use either, but you couldn't mix and match with Interface Builder. The change in Xcode 8 is that now you can: one and the same xib or storyboard file can contain views some of which use autoresizing and some of which use autolayout (as you rightly say in your point 2).
The advantage is just what you think it is: freedom of choice, plus easy of use: sometimes autoresizing is all you need, and it can be much faster and simpler to configure. For example, if you want a button to be in the top left corner and stay in the top left corner, you just drag a button from the Library into the top left corner and leave it there; the button's autoresizing configuration is top-left by default, and this configuration won't be turned into autolayout constraints, but will simply be left as is.
However, if you then make a constraint to that button, you have involved it in autolayout, it will now use autolayout, and you will have to add proper constraints for it. In the past, an autoresizing view was "translated" automatically by Interface Builder into an autolayout view if it became involved in autolayout, with automatically generated "autoresizing constraints", but that no longer happens — and this too is a good thing, as it makes it less likely that you'll end up with an accidental conflict between manual constraints and autoresizing constraints.

Subview that expands with window using NSAutolayout

I'm trying to learn auto layout so I can set up a moderately complicated display the way I want. I'm starting with a simple version. At least I thought it was simple.
I have a content view containing a NSScrollView, and a zoom slider. The scroll view is, of course, just a window into a larger 'canvas' on which the user can do things.
I'd like the scroll view to be as big as the window allows, with the slider underneath.
I've tried many things none of which work, in some cases when I resize the window smaller, the scroll view goes on up over the window's top bar, obscuring the title and the red yellow, green, dots.. this is just a grumble, I won't attempt to describe how I got it.
I'm working with Visual Format Language.
The immediate problem: I can only get the thing to work at all if I put in a hard size constraint on the scroll view.
I've got constraints like #"V:|[ScrollView]-[ZoomSlider(==35)]-| and
#"|-20#1000-[ScrollView]-|"
With these, nothing shows at all, until I put a hard size on the scroll view:
For example, #"V:[ScrollView(>=70#20)]" and #"[ScrollView(>=140#20)]" results in a little tiny scroll view (as expected) just above the slider.
Window is resizable, all right.
Is there a simple way to make the scroll view resize to occupy the most space possible when I resize the window? The only way I can think of off hand is to produce metrics for the scroll view based on window size, and use a notification to change the constraints when the window size changes. There should be something simpler!
THanks.ee
OOps. Thought I knew the answer until I started to write it.
AT least here is a partial explanation of things that were causing me problems.
You can't add constraints that position or size the Window's content view. But apparently you can mess them up by deleting them programatically. Some of my problems were solved by getting rid of
[self.myContentView setTranslatesAutoresizingMaskIntoConstraints:NO];
and
[self.myContentView removeConstraints:self.myContentView.constraints];
This left me with a lot of conflicting constraints. I fixed this be eliminating all content window constraints that I could in IB, then by marking the rest as placeholders.
I've got a ways to go before I understand how to use auto layout, but doing it in code is easier (for me) than doing it with IB

SplitView not resizing NSTableView in subview correctly

I have a 10.6 app that I am building on Lion with Xcode 4.3
There is a horizontal split view in the main view, containing the following:
The top view contains an NSSearchField with an NSTableView below it.
The bottom view contains a WebView.
I have it working, but when I resize the split view the top view behaves oddly.
What I want to happen is for the search field to remain where it is, the tableview to remain where it is, but to expand if the split view is dragged down. If dragged up, I want the webview to overwrite the search field and table view.
You can see what I mean in this clip: http://dl.dropbox.com/u/160638/Work/TENSOFT/resizemostlyokay.mov
This keeps the things in the right place when I drag up, but doesn't expand the table when I drag down. The view is expanded, but not the table.
So, I changed the autosizing constraint on the table view / scroll view to make it expand when the view is resized. This is what happens: http://dl.dropbox.com/u/160638/Work/TENSOFT/resizeproblem.mov
When the split bar is moved upwards the table view is moved upwards inside the top view until it overwrites the search field. It doesn't move back when the bar is moved back down.
I cannot find a way to make this work by changing the autosizing constraints. This is usually pretty easy stuff, so either I'm missing something obvious or...?
Has anyone seen this behaviour before when creating SL apps on Lion with Xcode 4.3?
FYI, if I replicate this in a new 10.7 project using auto-layout everything works fine.
Regards
Darren.
When you allow an NSSplitView to make one of its subviews very small so that the subviews effectively overlap you get layout issues and this is one of the reasons that Apple introduced auto-layout (watch the WWDC video about auto-layout and I think they demo this problem near the beginning).
If I were you I'd set a minimum size for the top pane so that, for example, it stops resizing when it is 100px high. You can then allow it to collapse so that the user can still show just the WebView.

Resources