I am trying to archive a similar behavior to what the Twitter for Mac App has.
It has TableViewCells of dynamic height that properly resize if the user resizes the whole window.
I started using an NSTableView with view based TableViewCells.
Now my problem is that I can calculate the initial height of a TableViewCell, but as soon as the user starts resizing the whole window, the height of course isn't correct any more and the tableview is't asking me again for a new height...
So my question is how I can solve this problem? Are TableViews the way to go?
I'd love it to work with AutoLayout somehow...
Thanks,
Georg
Related
Situation
I want to create a News-Article table within a macOS Cocoa application. (see image below)
Each Cell consists of two labels. One for the header and one for the main-content.
Problem
Now the length of the news-bodies and the width of the whole TableView is variable. I want to have a variable height on each Row/Cell depending on the size of the labels in it.
What we tried
We tried the following:
We have a
TableView
-> TableColumn
-> Custom View
-> LabelHeader
-> LabelBody
The RowSizeStyle Property on TableView is set to "Automatic"
The cells are populated with a custom class. Basically we tried to implement this StackOverflow solution: Link to Solution -> which did not work for us.
(And many more solutions we could not get to run)
Question
Can anyone provide a working tutorial or solution for this problem? Or at least a promising approach?
PS I use XCode 9.4.1 and Swift 4 on macOS 10.13.5
Any help gladly appreciated, Thank you!
In a modern cocoa application you would use NSLayoutConstraint to implement dynamic height of a table view cell. The tricky part would be to get the height of the text that you can set the height of the constrain. This could be done being using an NSTextView instead of NSTextField and asking the layout manager for the current height.
NSRect usedRect = [[textView layoutManager] usedRectForTextContainer:[self textContainer]];
float newHeight = usedRect.size.height;
I used this in an objective c application some years ago so if you have further questions please let me know.
My application has a NSTableView configured as a SourceList.
Under the NSTableView are two NSButton two add/remove items in the table.
The table and the buttons are embedded in a NSVisualEffectView (.behindWindow mode).
The result is great and I can see the background through the table and the buttons, BUT, when the app is in background (I give the focus to another app), the tableview and the buttons become black, and the view is not redrawn, as shown in the picture below (taken with an iPhone, because a screenshot does not show the bug!):
And here is the IB structure of the views:
The container view (CustomView) is layer-backed.
Any lead to solve this problem? Thanks in advance !
The problem was that I was trying to have a NSOutlineView translucent (Behind-window blending), but under a TabView.
Apple UI guidelines (here) state the following:
Use an opaque background when a window contains more than one sidebar, and when using a sidebar in a panel or preferences window. All other times, use a translucent background.
Because of the TabView, I was clearly against this rule which had technical side effects (which led to this post :-)).
--> making the NSOutlineView opaque (not as a SourceList) solved the problem.
Are any of your views opaque and implement drawRect:?
I've seen issues like this when a view implements drawRect: but doesn't completely fill the passed-in rect (or all of the rects returned from getRectsBeingDrawn:count:).
I am trying to understand Storyboard in XCode.
I have created a new Mac OS X Application from template and defined a simple Storyboard, like in the following image:
The problem is that when I run the application, my window looks like this:
I have to resize it to see RIGHT and LEFT part of vertical split view.
I have defined height and with of my window so I don't know why is this happening and I really don't know why is this window correctly shown with view controller below instead of split view controller.
What I am doing wrong with split view controller?
A solution is to add width constraints with a minimum width to the views in each side of the SplitView.
I got the idea from this answer to a similar question.
I'm having all kinds of trouble understanding how NSWindows can have larger documents than the window bounds in them.
Unfortunately, layout and contents prevents me from simply shrinking the document (and I wouldn't want to make the layout cramped for those with larger screens).
A school needs to run this app on their new 13", non-retina MacBook Pros. Scrolling is acceptable to them, but I'm unsure as to the approach, and I'd like your advice on the best way to handle this to avoid forced scrolling on larger screens.
I've tried setting the NSWindow min and max size and embedding the document in a Scroll View. But even though you can see part of the document view sticking out, no scroll bars appear (I have set them to Always in sys prefs).
If this is the way to go I would appreciate a link to a tutorial on this exact subject, because I'm a bit lost with all the measurements and options.
If not I'd like a pointer where to start and what to read. I'm experienced with Cocoa Touch but a relative newcomer to Mac development.
Without more information it sounds like you have embedded a NSScrollView but didn't set up the springs and struts properly to allow the scroll view to resize when its parent view (assuming it's the window) resizes.
You might want to check out Specifying a View’s Behavior as Its Container Resizes in the Interface Builder Help documentation.
In my application i am adding views to a window and i am setting windows size correctly to fit the views.But when i am trying to resize the windows the views in the window the views are not getting fitted depending on the window size one view gets overlapped with the other one and mismatchings. Can any one help me in this isssue by posting some sample how to do it.
i am new to mac development and got strucked up in this issue.
Thanks in advance
NSView has a setAutoresizingMask: method where you can specify how the view should resize, when its parent view changes size.
You can set this in code, but it might be easier to use Interface Builder.