> On Jul 20, 2015, at 4:45 PM, David Duncan <david.dun...@apple.com> wrote: > > >> On Jul 20, 2015, at 1:30 PM, Thomas Wetmore <t...@verizon.net> wrote: >> >> I really try to figure this stuff out. No kidding. I am trying this simple >> experiment: >> >> I define an NSView that has nothing more than a resize handle, defined as a >> rectangular NSBezierPath in its lower right corner. There are no subviews >> and no superviews involved here. I resize the NSView by implementing a mouse >> event loop in the NSView code. I also drag the NSView within its window >> within the same mouse event loop if the mouse down was not in the resize >> Bezier path. >> >> In the view’s initializer I create two NSLayoutConstraints, to set a minimum >> height and minimum width on the NSView, and add them to the view. I also set >> the NSView’s translatesAutoresizingMaskIntoConstraints property to false, so >> that these are the only two constraints defined on the the view. >> >> This is all done programmatically. There is no NIB for this NSView. For >> reference here is the code that creates and adds the constraints, found >> inside the view’s initializer: >> >> widthConstraint = NSLayoutConstraint(item: self, attribute: >> NSLayoutAttribute.Width, >> relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil, >> attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: >> 100.0) >> heightConstraint = NSLayoutConstraint(item: self, attribute: >> NSLayoutAttribute.Height, >> relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil, >> attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: >> 100.0) >> addConstraint(widthConstraint) >> addConstraint(heightConstraint) > > Nit: instead of using addConstraint(), you can use .active = true on the > constraints themselves and the constraints will find the most appropriate > view to add themselves to.
David, Thanks for that tip. I missed that in the docs. I added the constraints directly to the view itself as the view met the definition of “closest ancestor” as found in the docs. >> [Peeve, before I move on. I have to include the names of the two enum types >> (NSLayoutAttribute and NSLayoutRelation) in this code in order for the >> compiler to be happy — why? — I thought the compiler knows what the >> parameter types are so only the enum constants should be needed. End peeve.] >> >> When I instantiate one of these views and place it in an NSWindow, I can >> drag it around and I can resize it, as expected. Notably, however, I can >> resize it down to zero size (and even smaller!), even though the two >> constraints exist. I kind of expected the program to crash when the >> constraints were violated, but things keep on running. I can drag and resize >> the view at will with no repercussions. > > Since you’ve only given height & width constraints, you have an ambiguous > position for this view and no actual relation to the superview. Likely when > you get into this case the view is still respecting the size you gave it, but > since there is no relation to its parent, the parent will simply size smaller > and clip it. Ah. When I add the view to its window, it becomes a sub-view of the window’s content view. Yes, I understand that. However, when I add the view to the window, I have an algorithms that decides where to put it, so I define its location in the window programatically. I didn’t think about the ramifications of that with respect to the constraint system. I am now wondering whether I do have to. (Think of the window as a large working surface, and think of the views as note cards that can be moved around on the surface.) > If you add constraints to match this view to its superview’s > top/leading/bottom/trailing you will likely see the behavior you expect. > >> >> I would really like to be able to do resizing using a mouse event loop, but >> have the layout constraints somehow involved. Reading through references for >> NSView I don’t see how to do this. I guess I am looking for a way from >> within the event loop to be able to check whether a proposed new frame >> rectangle for the view obeys the view’s size constraints. >> >> Anyone have advice of what I should be reading to figure out what to do? All >> the examples I can find seem so simple. Whenever I get into one these, what >> seems to me to be a simple, real world situations (how unusual is a >> resizable view with constraints, after all?), I seem to get lost reading a >> plethora of confusing documents. It’s either me or the documentation, or >> maybe a lack of documentation. I can easily believe it’s me. But that >> doesn’t help me get this experiment to work. >> >> Clearly what I need is a better mental model of how the auto constraint >> system operates at run time. Maybe someone can point me in the direction of >> a good tutorial. >> >> Thanks, >> >> Tom Wetmore >> _______________________________________________ >> >> 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/david.duncan%40apple.com >> >> This email sent to david.dun...@apple.com > > -- > David Duncan > _______________________________________________ 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