Re: Auto Layout and Resizable NSViews
Re: Auto Layout and Resizable NSViews
- Subject: Re: Auto Layout and Resizable NSViews
- From: David Duncan <email@hidden>
- Date: Mon, 20 Jul 2015 13:45:42 -0700
> On Jul 20, 2015, at 1:30 PM, Thomas Wetmore <email@hidden> 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.
>
> [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.
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 (email@hidden)
>
> 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:
>
> This email sent to email@hidden
--
David Duncan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden