Re: How to get max size of view according to constraints?
Den 01:13 21. mars 2012 skrev Charles Srstka cocoa...@charlessoft.com følgende: On Mar 20, 2012, at 7:00 PM, Peter Ammon wrote: 2. Establish another constraint that says the height of the document view equals the height of the scroll view. Give this constraint a priority lower than required. What its priority should be depends on how tightly you want the document view to fill the scroll view, i.e. which other constraints it should be allowed to break. This sounds great in concept, but the height I want (if the constraints will allow it) is the scroll view’s documentVisibleRect, not its frame, since the latter includes the size of the scroll bars if they’re visible, and I don’t think that’s doable directly via constraints. However, watching the scroll view’s NSViewFrameDidChangeNotification and manually adding non-required constraints to the size I get from -documentVisibleRect (and clearing out those constraints the next time) works like a champ. As you point out, the mistake I was making before was calling setFrame: here instead of doing this via constraints. Thanks! I am old school and know nothing about constraints, but given your descriptions, perhaps you can tie the document view size to the clip view size (which is also called content view) instead of the scroll view size? Per ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 21, 2012, at 8:54 AM, Per Bull Holmen wrote: I am old school and know nothing about constraints, but given your descriptions, perhaps you can tie the document view size to the clip view size (which is also called content view) instead of the scroll view size? Actually, what I’m doing currently is to tie it to the clipview’s -documentVisibleRect.size, which is working quite well. Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
Den 18:09 21. mars 2012 skrev Charles Srstka cocoa...@charlessoft.com følgende: On Mar 21, 2012, at 8:54 AM, Per Bull Holmen wrote: I am old school and know nothing about constraints, but given your descriptions, perhaps you can tie the document view size to the clip view size (which is also called content view) instead of the scroll view size? Actually, what I’m doing currently is to tie it to the clipview’s -documentVisibleRect.size, which is working quite well. Charles But from how I understand what you've written, you are constantly listening for frame changes from the scroll view, and setting new constraints each time. I don't know how the constraints system works, but I understood it the way that you wouldn't have to do this if the scroll view's size was equal to the visible proportion of the document view. Therefore I was wondering how come you didn't set the constraints to follow the clip view instead of the scroll view, because I believe the clip view's frame will always have the same size as the visible portion of the document view. I think that would be much cleaner. I haven't done any programming with constraints, so I don't know if this will work. Per ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 21, 2012, at 12:52 PM, Per Bull Holmen wrote: Den 18:09 21. mars 2012 skrev Charles Srstka cocoa...@charlessoft.com følgende: On Mar 21, 2012, at 8:54 AM, Per Bull Holmen wrote: I am old school and know nothing about constraints, but given your descriptions, perhaps you can tie the document view size to the clip view size (which is also called content view) instead of the scroll view size? Actually, what I’m doing currently is to tie it to the clipview’s -documentVisibleRect.size, which is working quite well. Charles But from how I understand what you've written, you are constantly listening for frame changes from the scroll view, and setting new constraints each time. I don't know how the constraints system works, but I understood it the way that you wouldn't have to do this if the scroll view's size was equal to the visible proportion of the document view. Therefore I was wondering how come you didn't set the constraints to follow the clip view instead of the scroll view, because I believe the clip view's frame will always have the same size as the visible portion of the document view. I think that would be much cleaner. I haven't done any programming with constraints, so I don't know if this will work. It seems to be the same in testing; however, is it guaranteed to be that way anywhere in the documentation? The fact that NSClipView actually has a separate property named -documentVisibleRect seems to imply that it isn’t. Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
Den 19:26 21. mars 2012 skrev Charles Srstka cocoa...@charlessoft.com følgende: It seems to be the same in testing; however, is it guaranteed to be that way anywhere in the documentation? The fact that NSClipView actually has a separate property named -documentVisibleRect seems to imply that it isn’t. The -documentVisibleRect property gives the visible portion of the document in the document view's OWN coordinate system. The main difference in most cases will be the origin, which will be different from the frame of the clip view because of scrolling. If the document view's bounds are set in such a way that the drawing is magnified, that will also affect the size of -documentVisibleRect, because this rect is given in the document view's own coordinate system, while a view's frame is given in the super view's coordinate system. The whole purpose of the clip view is to clip the document view to show only the visible portion. Therefore, its size will always be equal to the visible prtion of the document view. It serves no purpose outside that area. It would be a strange decision if Apple should ever change that in the future, to let it serve as an invisible layer above other views, in addition to clipping the document view? I'd consider that bizarre, but I don't know if it says so explicitly anywhere in the docs. Per ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
Den 01:35 22. mars 2012 skrev Per Bull Holmen pbhol...@gmail.com følgende: The whole purpose of the clip view is to clip the document view to show only the visible portion. Therefore, its size will always be equal to the visible prtion of the document view. It serves no purpose outside that area. It would be a strange decision if Apple should ever change that in the future, to let it serve as an invisible layer above other views, in addition to clipping the document view? I'd consider that bizarre, but I don't know if it says so explicitly anywhere in the docs. Actually, I got curious about this, whether the Apple docs says so explicitly. I didn't find anywhere it said so in my brief search, but much of their own example code in their scroll view programming guide, relies on the size of the content view always being equal to the size of the visible portion of the document view. Per ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 19, 2012, at 10:20 PM, Charles Srstka wrote: As everyone knows, if you have a view with a bunch of subviews and you’ve got NSLayoutConstraints set up for everything, in many cases you might end up with a minimum or maximum size for the view beyond which the constraints are impossible to satisfy, and if you try to resize the view outside these bounds either in IB or in the actual program (if the view is the content view of a resizable window, for example), the resizing will simply stop at those boundaries. I have worked with constraints in another system and one of the things I learned is that a collection of constraints must be exercised or driven from one extreme to the other in order to have confidence that they are correct. If you have a min or max size condition for the view beyond which the constraints are impossible to satisfy, but this in not what you want, then you need to change the constraints so that you get what you want under all conditions. The constraint engine is mathematically correct and does not lie. So if there is a problem, it is in how you are specifying and arranging the constraints or in the number of constraints you have. --Richard ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 8:16 AM, Richard Somers wrote: On Mar 19, 2012, at 10:20 PM, Charles Srstka wrote: As everyone knows, if you have a view with a bunch of subviews and you’ve got NSLayoutConstraints set up for everything, in many cases you might end up with a minimum or maximum size for the view beyond which the constraints are impossible to satisfy, and if you try to resize the view outside these bounds either in IB or in the actual program (if the view is the content view of a resizable window, for example), the resizing will simply stop at those boundaries. I have worked with constraints in another system and one of the things I learned is that a collection of constraints must be exercised or driven from one extreme to the other in order to have confidence that they are correct. If you have a min or max size condition for the view beyond which the constraints are impossible to satisfy, but this in not what you want, then you need to change the constraints so that you get what you want under all conditions. The constraint engine is mathematically correct and does not lie. So if there is a problem, it is in how you are specifying and arranging the constraints or in the number of constraints you have. Right, but the problem is that I want to make a view that can be given arbitrary subviews at runtime, and I don’t necessarily know what its subviews and their constraints will be at compile time. What I want is a way to determine the range of sizes that this view can have, at runtime. I can find the minimum size via -fittingSize, but I can’t figure out how to get the maximum size. Specifically, what I’m trying to do is to make a constraints-aware NSScrollView. You can put whatever views you want in it, and it resizes its document view as appropriate as you resize the NSScrollView (say, by resizing the window it’s in). The idea is, it should attempt to resize the document view to match the size of the scroll view. If the user tries to resize the view smaller than what the constraints will allow, then the scroll bars appear. That part is working, but if the user tries to make the scroll view too big, then everything blows up when my code attempts to stretch the document view out. What I’m trying to find is the upper limit on the width and height according to the current set of constraints at runtime, when I don’t necessarily know what the subviews or their constraints are. Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Tue, Mar 20, 2012, at 12:01 PM, Charles Srstka wrote: Right, but the problem is that I want to make a view that can be given arbitrary subviews at runtime, and I don’t necessarily know what its subviews and their constraints will be at compile time. What I want is a way to determine the range of sizes that this view can have, at runtime. This is precisely the problem that the constraint system solves. You can't figure it out without actually solving all constraints. I can find the minimum size via -fittingSize, but I can’t figure out how to get the maximum size. Specifically, what I’m trying to do is to make a constraints-aware NSScrollView. You can put whatever views you want in it, and it resizes its document view as appropriate as you resize the NSScrollView (say, by resizing the window it’s in). The idea is, it should attempt to resize the document view to match the size of the scroll view. If the user tries to resize the view smaller than what the constraints will allow, then the scroll bars appear. That part is working, but if the user tries to make the scroll view too big, then everything blows up when my code attempts to stretch the document view out. Tried it. I don't believe it's possible. The whole point of scroll views (actually, clip views) is that the bounds coordinate space inside them is independent of the size of the view itself. If you start drawing constraints between the contents of the scroll view and its enclosing clip view, you are violating that principle. And it winds up being a moot point because -[NSScrollView tile] just calls -setFrame: directly on the clip view. What I’m trying to find is the upper limit on the width and height according to the current set of constraints at runtime, when I don’t necessarily know what the subviews or their constraints are. I do not believe this question makes sense from the perspective of the layout system. --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 1:03 PM, Kyle Sluder wrote: Tried it. I don't believe it's possible. The whole point of scroll views (actually, clip views) is that the bounds coordinate space inside them is independent of the size of the view itself. If you start drawing constraints between the contents of the scroll view and its enclosing clip view, you are violating that principle. And it winds up being a moot point because -[NSScrollView tile] just calls -setFrame: directly on the clip view. I’m not doing that part with constraints, but with code. It’s working perfectly, except when a view has a maximum size. What I’m trying to find is the upper limit on the width and height according to the current set of constraints at runtime, when I don’t necessarily know what the subviews or their constraints are. I do not believe this question makes sense from the perspective of the layout system. Sure it does. For example, take a view that has a maximum width, per its constraints, and make it the content view of an NSWindow. Now run the app and play around with resizing the window by dragging its edges. Notice how once you try to resize it to a width greater than the view’s maximum width, NSWindow stops resizing it any wider and keeps the width at the maximum. Somehow, NSWindow has figured out the maximum width of the view and adjusted its resize requests accordingly. How does it do that? Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 11:14 AM, Charles Srstka wrote: On Mar 20, 2012, at 1:03 PM, Kyle Sluder wrote: What I’m trying to find is the upper limit on the width and height according to the current set of constraints at runtime, when I don’t necessarily know what the subviews or their constraints are. I do not believe this question makes sense from the perspective of the layout system. Sure it does. For example, take a view that has a maximum width, per its constraints, and make it the content view of an NSWindow. Now run the app and play around with resizing the window by dragging its edges. Notice how once you try to resize it to a width greater than the view’s maximum width, NSWindow stops resizing it any wider and keeps the width at the maximum. Somehow, NSWindow has figured out the maximum width of the view and adjusted its resize requests accordingly. How does it do that? It ran the constraints system, solved for the maximum size of the content view, and prevented you from resizing the NSWindow. But it knew what all the subviews were: the empty set. That's not the same as solving the constraints system without regard to subviews. --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 1:21 PM, Kyle Sluder wrote: It ran the constraints system, solved for the maximum size of the content view, and prevented you from resizing the NSWindow. But it knew what all the subviews were: the empty set. That's not the same as solving the constraints system without regard to subviews. Sure it is. The developers of the NSWindow object had no idea what views I was going to put into it, so they coded it to dynamically determine its min and max size based on what views it contains at runtime. I’d like to do the same thing with my view class. Running the constraints system, solving for the maximum size of the content view, and preventing the view from being resized any larger than that is *precisely* what I’d like to do. Is there any way to do this (short of iterating through the constraints and doing the math manually, which is not likely to be future-proof given that Apple could add new constraint types in the future that my view would’t know about)? Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 11:46 AM, Charles Srstka wrote: On Mar 20, 2012, at 1:21 PM, Kyle Sluder wrote: It ran the constraints system, solved for the maximum size of the content view, and prevented you from resizing the NSWindow. But it knew what all the subviews were: the empty set. That's not the same as solving the constraints system without regard to subviews. Sure it is. The developers of the NSWindow object had no idea what views I was going to put into it, so they coded it to dynamically determine its min and max size based on what views it contains at runtime. I’d like to do the same thing with my view class. NSWindow runs the constraint system on its view hierarchy, and then constrains the size of the window the size computer for its content view. Running the constraints system, solving for the maximum size of the content view, and preventing the view from being resized any larger than that is *precisely* what I’d like to do. Is there any way to do this (short of iterating through the constraints and doing the math manually, which is not likely to be future-proof given that Apple could add new constraint types in the future that my view would’t know about)? The formulation of your question led me to believe you wanted to run the constraints system on a view without care for the subviews it contained. Of course NSWindow doesn't know what's contained within its contentView; including that bit in your question is a bit misleading. Does calling -layoutSubtreeIfNeeded not do what you want? --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 2:26 PM, Kyle Sluder wrote: NSWindow runs the constraint system on its view hierarchy, and then constrains the size of the window the size computer for its content view. Yes! I’d like a way to do the same thing, and get at that computed size. Running the constraints system, solving for the maximum size of the content view, and preventing the view from being resized any larger than that is *precisely* what I’d like to do. Is there any way to do this (short of iterating through the constraints and doing the math manually, which is not likely to be future-proof given that Apple could add new constraint types in the future that my view would’t know about)? The formulation of your question led me to believe you wanted to run the constraints system on a view without care for the subviews it contained. Of course NSWindow doesn't know what's contained within its contentView; including that bit in your question is a bit misleading. Since I explicitly said I don’t know what views are going to be there _at compile time_, I don’t think it’s actually misleading at all. The reason I put that bit there was because the first person who replied misunderstood my question and thought I was doing something with a fixed set of subviews. Does calling -layoutSubtreeIfNeeded not do what you want? Not really. Let’s say we have an NSView with the following size limitations imposed by its subviews: 100 = width = 300 200 = height = 400 If you have this view open in Interface Builder, you can drag it around to resize it, and IB will let you give it any size in between { 100, 200 } and { 300, 400 }. If you try to make the width smaller than 100, it will change the width to 100, and if you try to make the width larger than 300, then it will set the width to 300, but within those limitations, it will let you make the view size anything you want. This is what I’m trying to do. The -layoutSubtreeIfNeeded method, in contrast, always seems (at least in my testing) to change everything to the minimum allowed size. So for the example above, if you set the view to have the size { 200, 300 }, it would change that to { 100, 200 }, despite { 200, 300 } being a perfectly legal size under those constraints. I don’t want to find what the system thinks is the “one true” size — I’m just trying to figure out the limits so I can keep the sizes legal, that’s all. Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On 20 Mar 2012, at 21:12, Charles Srstka wrote: The -layoutSubtreeIfNeeded method, in contrast, always seems (at least in my testing) to change everything to the minimum allowed size. So for the example above, if you set the view to have the size { 200, 300 }, it would change that to { 100, 200 }, despite { 200, 300 } being a perfectly legal size under those constraints. I don’t want to find what the system thinks is the “one true” size — I’m just trying to figure out the limits so I can keep the sizes legal, that’s all. Charles I am getting a bit befuddled. Is the following what we have: 1. A certain view hierarchy managed by a set of constraints. 2. A certain Charles, who wishes to programatically modify (maximise?) the size of one (or more) of the managed views in such a way that it doesn't cause a layout exception. 3. In order to perform 2, Charles wants to be able to query the layout for a given view to determine its (maximum?) allowed size. Regards Jonathan Mitchell Mugginsoft LLP ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 4:29 PM, jonat...@mugginsoft.com wrote: I am getting a bit befuddled. Is the following what we have: 1. A certain view hierarchy managed by a set of constraints. Yes. 2. A certain Charles, who wishes to programatically modify (maximise?) the size of one (or more) of the managed views in such a way that it doesn't cause a layout exception. Close. A certain Charles, who wishes to make a generalized view that will allow the user to resize it as he or she wishes, but needs to make sure the size is legal in order to avoid throwing exceptions, flooding the console with warnings, and otherwise being bad. And since this is a generalized view, the certain Charles needs to cover the corner cases, one being when the view has a maximum size. 3. In order to perform 2, Charles wants to be able to query the layout for a given view to determine its (maximum?) allowed size. Yes, in order to perform 2, a certain Charles wants to be able to query for both the minimum and maximum size (he’s already got the minimum), so he can be sure the size that the user wants to set is within those boundaries. This way, if a bad size comes from the user, the certain Charles can adjust it. But in these situations, Charles wants to make the size as close to what the user asked for as is possible. Think what happens when a certain Jonathan resizes a view with constraints by dragging its edges around in IB. That’s what Charles wants to do. Ugh, too much use of the third person makes Charles nauseous. Not going to do that again. :-D Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
Hello Charles, It sounds like you have the idea that constraints establish a minimum and maximum size on a window (or view), and the window is free to take on any size within that range. When you resize a window, it evaluates the constraints at the proposed size. If would make the constraints invalid then it limits the size. This isn't quite right. In fact, a window has the size it has because it is constrained to be exactly that size. If there is wiggle room we would say the constraints are ambiguous because they do not fully specify the size. That's considered a programming error. As the user resizes a window by dragging it, AppKit modifies those constraints. These constraints have the priority NSLayoutPriorityWindowSizeStayPut. If there are other constraints with higher priority, they take precedence, and will limit the window size. There is no explicit max size calculation: the max size falls out of the constraints. So to answer your question, in most cases it is a bad idea to try to figure out the maximum size of a view. It is also a bad idea to call setFrame:, because that will be immediately undone the next time -layout runs. Instead, you should create width and height constraints for the view, set them, call -layoutIfNeeded, and then inspect the resulting size. If you want to allow certain other constraints to limit the size, then set a priority for your constraint that is lower than that of those other constraints. By the way, fittingSize does not correspond to the view's minimum size, for the same reason. Hope that helps, -Peter On Mar 19, 2012, at 9:20 PM, Charles Srstka cocoa...@charlessoft.com wrote: As everyone knows, if you have a view with a bunch of subviews and you’ve got NSLayoutConstraints set up for everything, in many cases you might end up with a minimum or maximum size for the view beyond which the constraints are impossible to satisfy, and if you try to resize the view outside these bounds either in IB or in the actual program (if the view is the content view of a resizable window, for example), the resizing will simply stop at those boundaries. What I am having trouble figuring out is how to resize such a view in code in such a way that it will respect the constraints. If I try just setting a frame using -setFrame: with a rect which has an illegal size, NSView is all too happy to do that, and then I get an exception and a flood of console warnings about how I just broke my constraints. Now, getting the view’s minimum size by calling -fittingSize and adjusting my frame accordingly is easy enough, but some views can have a maximum size imposed by constraints as well, and I’m having trouble finding a way to find out what it is in code. I know that this has to be possible, since Interface Builder and NSWindow are both able to gracefully handle cases where one tries to set a frame size outside the boundaries of what the constraints allow, but I haven’t been able to find any -maximumSize or -adjustedSizeForSize: type methods, or any way to figure this out short of wrapping -setFrame: in an exception handler. I’m sure it’s gotta be something fairly obvious, and I’ll probably be embarrassed by the answer, but at the moment I’m a bit stuck. Anyone know how to do this? Thanks, Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
SOLVED Re: How to get max size of view according to constraints?
Sweet! I’ve found the solution to my own problem. Here it is, in case someone else finds this thread wondering the same thing. What I was trying to do was to keep track of the NSScrollView being resized, and adjust the size of its document view as needed. Toward this end, I was trying to determine the min and max allowed size, grab the scroll view’s documentVisibleRect, adjust the size to avoid angering the constraint gods, and then set the frame on the document view. I had figured the solution to this would be something fairly simple and embarrassing, and sure enough it was. During a rewatching of the WWDC 2011 video on Cocoa Auto Layout in order to figure out the cause of some other weird bugs I was seeing, I noticed something I’d glossed over before: when using constraints, you are not supposed to call setFrame: on any of your views, but are instead supposed to set constraints that tell the autolayout system what the position and size should be. And of course, once I set about doing that, I remembered that these constraints can have priorities — so, all I had to do was to set the width and height to what I wanted them to be with their priorities set to NSLayoutPriorityDragThatCanResizeWindow, and all’s well! The view gets the size I tell it if it’s legal, and adjusts it if it’s not. Perfect. Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 10:01 AM, Charles Srstka wrote: On Mar 20, 2012, at 8:16 AM, Richard Somers wrote: On Mar 19, 2012, at 10:20 PM, Charles Srstka wrote: As everyone knows, if you have a view with a bunch of subviews and you’ve got NSLayoutConstraints set up for everything, in many cases you might end up with a minimum or maximum size for the view beyond which the constraints are impossible to satisfy, and if you try to resize the view outside these bounds either in IB or in the actual program (if the view is the content view of a resizable window, for example), the resizing will simply stop at those boundaries. I have worked with constraints in another system and one of the things I learned is that a collection of constraints must be exercised or driven from one extreme to the other in order to have confidence that they are correct. If you have a min or max size condition for the view beyond which the constraints are impossible to satisfy, but this in not what you want, then you need to change the constraints so that you get what you want under all conditions. The constraint engine is mathematically correct and does not lie. So if there is a problem, it is in how you are specifying and arranging the constraints or in the number of constraints you have. Right, but the problem is that I want to make a view that can be given arbitrary subviews at runtime, and I don’t necessarily know what its subviews and their constraints will be at compile time. What I want is a way to determine the range of sizes that this view can have, at runtime. I can find the minimum size via -fittingSize, but I can’t figure out how to get the maximum size. Specifically, what I’m trying to do is to make a constraints-aware NSScrollView. You can put whatever views you want in it, and it resizes its document view as appropriate as you resize the NSScrollView (say, by resizing the window it’s in). The idea is, it should attempt to resize the document view to match the size of the scroll view. If the user tries to resize the view smaller than what the constraints will allow, then the scroll bars appear. That part is working, but if the user tries to make the scroll view too big, then everything blows up when my code attempts to stretch the document view out. What I’m trying to find is the upper limit on the width and height according to the current set of constraints at runtime, when I don’t necessarily know what the subviews or their constraints are. Here's how I'd recommend approaching the scroll view example via constraints. 1. Establish a required constraint that says your document view's top equals the scroll view's top. 2. Establish another constraint that says the height of the document view equals the height of the scroll view. Give this constraint a priority lower than required. What its priority should be depends on how tightly you want the document view to fill the scroll view, i.e. which other constraints it should be allowed to break. By choosing a priority lower than Required, it will not break required constraints amongst the subviews, and so it will not cause exceptions. You should be able to do all this without ever calling setFrame: or fittingSize. Hope this helps. Feel free to ask more questions. Auto layout is very powerful, but requires a different way of thinking than the old setFrame: approach. -Peter ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to get max size of view according to constraints?
On Mar 20, 2012, at 7:00 PM, Peter Ammon wrote: 2. Establish another constraint that says the height of the document view equals the height of the scroll view. Give this constraint a priority lower than required. What its priority should be depends on how tightly you want the document view to fill the scroll view, i.e. which other constraints it should be allowed to break. This sounds great in concept, but the height I want (if the constraints will allow it) is the scroll view’s documentVisibleRect, not its frame, since the latter includes the size of the scroll bars if they’re visible, and I don’t think that’s doable directly via constraints. However, watching the scroll view’s NSViewFrameDidChangeNotification and manually adding non-required constraints to the size I get from -documentVisibleRect (and clearing out those constraints the next time) works like a champ. As you point out, the mistake I was making before was calling setFrame: here instead of doing this via constraints. Thanks! Charles ___ Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com This email sent to arch...@mail-archive.com