Re: Auto Layout and Resizable NSViews
Re: Auto Layout and Resizable NSViews
- Subject: Re: Auto Layout and Resizable NSViews
- From: Ken Thomases <email@hidden>
- Date: Mon, 20 Jul 2015 16:13:55 -0500
> On Jul 20, 2015, at 3:30 PM, Thomas Wetmore <email@hidden> wrote:
> 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.
> 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.
>
> 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.
What you should do when using auto layout is place and size the view using constraints. Thus moving the view involves modifying the placement constraints and resizing the view involves modifying the size constraints. For the size constraints, they should be lower priority than your minimum-size constraints (if you want to keep those separate). Therefore, no matter what the drag-tracking code does to those size constraints, your view still won't go below the minimum size. Of course, a reasonable alternative is to just use the one set of size constraints and, in your code, disallow ever setting them below your minimums. You'll have to do something like that, anyway, to prevent the size constraint constants from going negative, since that's not allowed.
You should avoid using any of the -setFrame… methods. I assume that's what you have been using to place and size the view, but frames set that way get overridden by the next layout pass.
Regards,
Ken
_______________________________________________
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