Discussion:
How to resize accessory view to match Open panel
(too old to reply)
Jonathan Taylor
2015-07-08 15:33:55 UTC
Permalink
I feel this should be a simple question, but I cannot find an answer that works.

I have an open panel to which I am trying to add an accessory view. That much works. However I would like the accessory view to resize to fit the width of the parent window. It’s just a textual description, after all.

An old thread here:
http://www.cocoabuilder.com/archive/cocoa/106875-setaccessoryview.html
suggests this is not trivial to do.

I have tried the suggested monitoring of NSWindowDidResizeNotification (and also tried NSViewFrameDidChangeNotification), and these don’t seem to work quite right. There have been plenty of OS changes since that thread was written, so probably no surprises there.

What happens is that it doesn’t seem possible to *shrink* the Open window (I guess the ‘hard’ size that I am setting for my view is affecting the minimum size of the overall window) and, much more alarmingly, I get crashes when resizing.

The alternative suggestion related to IB struts doesn’t seem to work for me either. I set all four edge struts and tried both with and without the ‘resizeable’ arrows in the middle of the view. No effect.

Can anyone advise on the current and correct way of getting the accessory view to resize appropriately? It seems this should be a common desired behaviour, but haven’t had any luck doing it or finding any recent examples/advice on google…

Thanks
Jonny
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

Thi
Richard Charles
2015-07-08 16:14:35 UTC
Permalink
Post by Jonathan Taylor
I have an open panel to which I am trying to add an accessory view. That much works. However I would like the accessory view to resize to fit the width of the parent window. It’s just a textual description, after all.
I have a preference panel window controller that adjusts the panel height based on which toolbar item is selected. You can see the same thing in the Preferences panel for a number of Apple’s applications (Mail, Preview, Safari). The understanding of how to do this came from "Cocoa Programming for OS X" Chapter 31 View Swapping and Custom Container View Controller by Hillegass.

There may be some similarities there to what you are doing.

--Richard Charles


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email s
Lee Ann Rucker
2015-07-08 21:44:54 UTC
Permalink
My accessory views use autolayout and that seems to work fine in 10.9 and up. In 10.8, you need

[accessoryView layoutSubtreeIfNeeded];
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_8) {
[accessoryView setTranslatesAutoresizingMaskIntoConstraints:YES];
}
Post by Jonathan Taylor
I feel this should be a simple question, but I cannot find an answer that works.
I have an open panel to which I am trying to add an accessory view. That much works. However I would like the accessory view to resize to fit the width of the parent window. It’s just a textual description, after all.
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.cocoabuilder.com_archive_cocoa_106875-2Dsetaccessoryview.html&d=BQIGaQ&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=ie7S-J__EKnfyVOBV7-jV2rZ--p47O6vkyTklpDM3h4&m=cHbQmyTJpg2XJFwD6Ij_z3NjqhIEgWWn9zXrnGjjOxk&s=MuBFc9loNUfef9MsVOB-rZbhMfX4JwFpjj8iB_S-OzU&e=
suggests this is not trivial to do.
I have tried the suggested monitoring of NSWindowDidResizeNotification (and also tried NSViewFrameDidChangeNotification), and these don’t seem to work quite right. There have been plenty of OS changes since that thread was written, so probably no surprises there.
What happens is that it doesn’t seem possible to *shrink* the Open window (I guess the ‘hard’ size that I am setting for my view is affecting the minimum size of the overall window) and, much more alarmingly, I get crashes when resizing.
The alternative suggestion related to IB struts doesn’t seem to work for me either. I set all four edge struts and tried both with and without the ‘resizeable’ arrows in the middle of the view. No effect.
Can anyone advise on the current and correct way of getting the accessory view to resize appropriately? It seems this should be a common desired behaviour, but haven’t had any luck doing it or finding any recent examples/advice on google…
Thanks
Jonny
_______________________________________________
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
https://lists.apple.com/mailman/options/cocoa-dev/lrucker%40vmware.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in
Jonathan Taylor
2015-07-09 12:05:15 UTC
Permalink
Thanks very much for your reply. I did wonder if autolayout might make life easier - that’s something I haven’t delved into before.

I gave it a try, setting what I thought should be needed, but it doesn’t seem to be having the desired effect as yet. I have the nib file set to “Use autolayout”, and have included the code you quoted. Doesn’t seem to have any effect. I am loading a nib which contains just an NSTextField, which I have given a red background to make it clear what its extent is. As you can see from this screenshot, the text field is retaining its shape as defined in the nib and is not resizing as I would like it to, to match the size of the parent window.
http://imgur.com/aWN4cGU
It may be that I am doing something wrong related to autolayout (although this was exactly the problem I was having with non-autolayout as well…). Do you have any thoughts about what I might be missing in order for this to behave as I would like?

Cheers
Jonny
Post by Lee Ann Rucker
My accessory views use autolayout and that seems to work fine in 10.9 and up. In 10.8, you need
[accessoryView layoutSubtreeIfNeeded];
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_8) {
[accessoryView setTranslatesAutoresizingMaskIntoConstraints:YES];
}
Post by Jonathan Taylor
I feel this should be a simple question, but I cannot find an answer that works.
I have an open panel to which I am trying to add an accessory view. That much works. However I would like the accessory view to resize to fit the width of the parent window. It’s just a textual description, after all.
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.cocoabuilder.com_archive_cocoa_106875-2Dsetaccessoryview.html&d=BQIGaQ&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=ie7S-J__EKnfyVOBV7-jV2rZ--p47O6vkyTklpDM3h4&m=cHbQmyTJpg2XJFwD6Ij_z3NjqhIEgWWn9zXrnGjjOxk&s=MuBFc9loNUfef9MsVOB-rZbhMfX4JwFpjj8iB_S-OzU&e=
suggests this is not trivial to do.
I have tried the suggested monitoring of NSWindowDidResizeNotification (and also tried NSViewFrameDidChangeNotification), and these don’t seem to work quite right. There have been plenty of OS changes since that thread was written, so probably no surprises there.
What happens is that it doesn’t seem possible to *shrink* the Open window (I guess the ‘hard’ size that I am setting for my view is affecting the minimum size of the overall window) and, much more alarmingly, I get crashes when resizing.
The alternative suggestion related to IB struts doesn’t seem to work for me either. I set all four edge struts and tried both with and without the ‘resizeable’ arrows in the middle of the view. No effect.
Can anyone advise on the current and correct way of getting the accessory view to resize appropriately? It seems this should be a common desired behaviour, but haven’t had any luck doing it or finding any recent examples/advice on google…
Thanks
Jonny
_______________________________________________
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
https://lists.apple.com/mailman/options/cocoa-dev/lrucker%40vmware.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent
Ken Thomases
2015-07-09 15:18:03 UTC
Permalink
Post by Jonathan Taylor
I gave it a try, setting what I thought should be needed, but it doesn’t seem to be having the desired effect as yet. I have the nib file set to “Use autolayout”, and have included the code you quoted.
Even in a NIB set to use auto layout, top-level views have translatesAutoresizingMaskIntoConstraints turned on. So, you should turn it off in the NIB on the Attributes inspector or in code after you've loaded it, if the open panel is expected to be auto-layout-savvy. (In general, actually, one should leave it on and let the code which places the view into a larger hierarchy decide whether to turn it off. But maybe turning it off will work for this case. I don't know.)

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net
Jonathan Taylor
2015-07-09 16:54:46 UTC
Permalink
This post might be inappropriate. Click to display it.
Jonathan Taylor
2015-07-10 11:21:35 UTC
Permalink
Well, I think I have bumbled my way towards a working solution that seems to work (tested on OS 10.9), although this was largely through trial and error and so might not be the best way of doing things. Thankyou very much to everyone who offered help on and off list.

One indirect clue I found was here:
http://stackoverflow.com/questions/27374355/nssavepanel-crashes-on-yosemite
(I encountered the same issue if I set translatesAutoresizingMaskIntoConstraints=NO in the nib itself)

Another was the failure of my attempts to manually set my own constraints on my view, after adding it to the NSOpenPanel. As far as I can tell, the act of adding it causes NSOpenPanel to apply some fairly aggressive auto-constraints, and I didn’t manage to work out how I could override them. That might somewhat explain why my attempts to change things in my nib file weren’t having much effect - I suspect that NSOpenPanel is specifically overriding certain properties with ones it has chosen itself.

Anyway, long story short: AFTER adding the view as openPanel.accessoryView, I manually set the view rectangle to fill its parent view, and then I do:
viewController.view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; // Must be done AFTER adding as accessory view
This seems to result in the behaviour I want (my view fills the available space, and scales as the window is resized). This works regardless of whether the nib itself is set to autolayout or not.

Hopefully that information might be of some use for posterity.

Cheers
Jonny
Post by Ken Thomases
Post by Jonathan Taylor
I gave it a try, setting what I thought should be needed, but it doesn’t seem to be having the desired effect as yet. I have the nib file set to “Use autolayout”, and have included the code you quoted.
Even in a NIB set to use auto layout, top-level views have translatesAutoresizingMaskIntoConstraints turned on. So, you should turn it off in the NIB on the Attributes inspector or in code after you've loaded it, if the open panel is expected to be auto-layout-savvy. (In general, actually, one should leave it on and let the code which places the view into a larger hierarchy decide whether to turn it off. But maybe turning it off will work for this case. I don't know.)
Regards,
Ken
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email s

Continue reading on narkive:
Loading...