Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I align a view to the top-left side of a window?

When I place a button at the top-left side of the window, it aligns exactly like I would expect:

http://stuf.ro/Button.png

It also doesn't resize along with the window, which is good. If I try to do the same thing with a custom view however, it automatically adds constraints to the bottom and to the right, which makes the view resize along with the window:

http://stuf.ro/View.png

How do I get rid of these? I'd like the view to never resize, and I'd like it to be pinned to the top-left side of the window, just like a button.

If I try to select the constraint and press the Backspace key, nothing happens. The "Delete" option in the edit menu is disabled as well.

like image 591
rid Avatar asked Apr 12 '12 14:04

rid


People also ask

What is the shortcut to move a window to the left side on a Mac?

Move a window to one side of the screen: Press and hold the Option key while you move the pointer over the green button in the top-left corner of the window, then choose Move Window to Left Side of Screen or Move Window to Right Side of Screen from the menu that appears.

How do you move a window to the side on a Mac?

macOS Catalina or laterHold your pointer over the full-screen button in the upper-left corner of a window. Or click and hold the button. Choose ”Tile Window to Left of Screen” or ”Tile Window to Right of Screen” from the menu. The window then fills that side of the screen.

How do I arrange open windows on my desktop?

To optimize your screen space and your productivity, hover over a window's maximize button or select a window and press Win+Z, then choose a snap layout. Use Snap to arrange all your open windows using the mouse, keyboard, or the Snap Assist feature.


1 Answers

Your view resizes because it has constraints saying the space above it, below it, to its left, and to its right should be fixed. These are the lines you can see around your view in the screenshot. The only way to satisfy these constraints as the window resizes is for the view to resize.

You can select the constraint lines to modify or delete them. You might think you could delete the ones below and to the right, and your view will no longer need to resize to satisfy the remaining constraints, but that doesn't work. The view needs a (set of) constraints that specify both size and spacing: having the (automatically created) spacing constraints on either side implies size, but if you got rid of one of them the size would no longer be specified. (This is why you can't get rid of automatically created constraints -- the ones which appear as narrow blue lines in the view and with purple icons in the document outline.)

This isn't a problem for the button because buttons know how to size themselves, and IB knows about how buttons size themselves. IB doesn't know about your custom view, so you have to set up the constraints yourself. With the view selected, choose Editor > Pin > Width to create a width constraint. This both creates a width constraint and turns the existing spacing constraints into user constraints (as opposed to automatic ones) -- they appear as solid lines and have blue icons in the document outline.

Now you can select the spacing constraint on the right and delete it, and your view will keep its width and stick to the left. Repeat for height and space below and your view will stay in the upper left and not resize.

You can read more about the new auto layout system in Apple's guide.

like image 179
rickster Avatar answered Oct 09 '22 06:10

rickster