Use auto layout to position button near the center - xcode

I'm using auto layout in Xcode and I have some buttons I want to be near the center, but not on it. I know I can use alignment constraints to be exactly in the center, in this menu:
However, I want to not be exactly in the center, lets say 50 pixels up and to the right. How can I do this? I'm sure it's an easy thing to do, but I can't seem to find anything elsewhere on the web.

If you want the center of your button to be 50 points up and to the right of the parent view's center you can use a center x alignment constraint and a center y alignment constraint and set the center x alignment constraint's constant to 50 and the center y alignment constraint's constant to -50.

Create a view with size 50 * 50. Then place this view exactly at the center using auto layout.
Next you need to place your button using vertical space and horizontal space in order to be aligned to the upper right corner of the view.
Finally make the view transparent.

Related

Adding controller buttons to prompt text?

I want to make a controller button prompt in my game where it says 'Press X to Join', but I want the X to be an actual graphic of the 'X' button, like how it is in many games. How would I go about doing that? Right now I'm just putting a big space in my prompt text and putting a UI Image of the button in it, but I want to know if there's a better way about it.
For the sake of scaling to different resolution sizes, you would wanna scale the anchors of the UI elements correctly and have an appropriate parent-child relation tree in Unity's hierarchy.
The red box is the hierarchy.
The 2 green boxes shows ways of scaling the anchors.
The orange box shows the end result of it.
Anchors in combination with their relative position, allows Unity's UI elements to scale up and down according to screen size.
So for example if you say that an anchor is at 0.25x, that would be that its anchored at 25% of for example the x axis. Same goes if you set it for the y axis, just the vertical instead of horizontal anchoring.
You can use the anchors to adjust a minimum and a maximum anchor which the elements may float within, they may have the same value as well, then it's a fixed anchor point.
To clarify, I suggest that you use a panel to hold 2 text elements and the image with the X, each text element being on the left and right side of the X instead of having spacing inside the UI text elements. To keep correct spacing you then must use anchor points. This way your spacing stays correct despite changing screen and resolution sizes.
Please note that the "left", "top", "right" and "bottom" values are then relative to the anchor points. So if you move "left" 5 pixels, those 5 pixels will be out from the relative anchor point.
Here's the values I used:
My left text is at 0.25x, right text is at 0.8x, image is at 0.5x.
The panel holding the 3 is at 0.2 minimum x to 0.8 max x, same goes for y axis.
The largest parent panel is stretched to max fit in the canvas.

Xcode 8 Auto Layout Constraints not working as expected. Very basic setup with centering plus an issue with pins. Video attached

I am having trouble getting a grasp on how auto-layout constraints work. In theory, one should be able to add a label to the storyboard, then assign a horizontal centre constraint and it should be centred on all devices, correct? However, when I do that, it does NOT stay centred UNLESS I also add a vertical spacing constraint. However, if I test the app it IS centred (without the vertical spacing constraint). This is causing me great confusion.
Secondly, if I add a text input, expand it to full width, then add pin the left and right side, it does NOT maintain the pins in other views. And instead there is this dotted box inside a solid orange line box which is centred, but the element itself is not.
I am at a loss please help. I have attached a video of me doing it in Xcode. Both issues are in there First the centring issue, next to the pinning issue. Thank you!
VIDEO OF THE ISSUES HERE:
https://vid.me/XxFo
-- Red lines mean Constraints are ambiguous. Yellow means warning, Its show you the runtime demonstration of view.
View need four constraint to satisfy its full position.
x, y, width and height
When you give only center horizontally constraint, this is not enough you have to specify the y position as well. Label get its width and height from its text length and text font size.
Though constraints are ambiguous and in preview you are not getting correct result but once you give center constraint, it will be center at runtime.
Run the app and it should be in center horizontally.

Difference between Frame Rectangle and Alignment Rectangle

Can anyone explain what is the difference between Frame Rectangle and Alignment Rectangle in the size inspector?
Frames describe where to place views on the screen and how big those views will be. When laying out views, constraints use a related geometric element called an alignment rectangle.
The alignment rectangle is based on the presentation of the item’s content, Auto Layout uses the alignment rectangle instead of the item’s frame rectangle.
By working with alignment rectangles instead of frames, Auto Layout ensures that key information like a view’s edges and center are properly considered during layout.
Unlike frames, a view’s alignment rectangle should be limited to a core visual element. Its size should remain unaffected as new items are drawn onto the view.
Consider the left side of Figure -1. It shows a view drawn with a shadow and a badge. When laying out this view, you want Auto Layout to focus on aligning just the core element—the blue rectangle—and not the ornamentation.
Figure - 1 : A view’s alignment rectangle (center) refers strictly to the core visual element to be aligned, without embellishments.
The center image in Figure -1 highlights the view’s alignment rectangle. This rectangle excludes all ornamentation, such as the drop shadow and badge. It’s the part of the view you want Auto Layout to consider when it does its work.
The right-hand rectangle in Figure -1 encompasses all the view’s visual elements. It encompasses the shadow and badge. These ornaments could potentially throw off a view’s alignment features (like center, bottom, and right) if they were considered during layout.
Figure -2 Auto Layout only considers this view’s alignment rectangle when laying it out as centered in its superview. The shadow and badge don’t affect its placement.
Reference: link
From Apple's old Auto Layout Guide:
Layout Operates on Alignment Rectangles, Not on Frames
When considering layout, keep in mind that the frame of a control is
less important than its visual extent. As a result, ornaments such as
shadows and engraving lines should typically be ignored for the
purposes of layout. Interface Builder ignores them when positioning
views on the canvas—in the following example, the Aqua guide (the
dashed blue line) aligns with the visual extent of the button, not
with the button’s frame (the solid blue rectangle).
To allow layout based on the presentation of the content rather than
the frame, views provide an alignment rectangle, which is what the
layout actually operates on. To determine whether your override is
correct on OS X, you can set the NSViewShowAlignmentRects default to
YES to draw the alignment rects.
So, that pop-up menu in Xcode is providing you the ability to view and edit a view's size and position based on its alignment rect or its frame rectangle.

About cutting of / cropping a uiview

so i have a uiview that is initialized with a frame that has the height and width that is present for the user, i want the user to be able to draw inside this frame but when the user presses a button, i want the view to cut off that extra wasted space so that the frame is only as big as what the user was drawing. I tried to do something like this
CGRect boundbox = CGPathGetBoundingBox([myPath CGPath]);
boundbox.origin.x = self.frame.origin.x;
boundbox.origin.y = self.frame.origin.y;
self.frame = boundbox;
However, this does not remove that extra wasted space, it only resizes the view, so that the drawn content looks smaller than previously. What i would like to do instead is to remove
that "whitespace", i was thinking if it could be possible to scale up the content of the uiview, but im not sure.
To clarify what i mean:
The red border is the area / frame that the user can draw on, the text in the middle is a drawing, when the user presses a button, i want the frame to only encircle the drawing like in figure 2.
Now lets say i have the following scenario, i have drawn a circle on the middle of the screen.
When i then press the button, the scale remains the same but the circle is still in the same position but we have now changed the draw area, so the circle / drawing will look like its cut off like in figure 4.
What i want to do is to move the drawing / bezier path so that it is positioned in the middle of the frame. So that the red area encircles the blue circle.
[EDIT]
Given your drawings. A UIView will not re-position items in it when you change it's frame property (or it's CGRect). In this case you will need to track the items drawn YOURSELF, and then when the button is pressed perform the object translations yourself.
What that means is you will have to find the object that is left most, the object that is topmost, then move all objects left by that amount, and up by that amount so that all objects are (as a grouping) top-left aligned within the view's frame. After this you will need to self recognize which object is the right most touching and which object is the bottom most touching.
NOW, since you have already moved the items left-top, the right most point will define your frame width, and the bottom most point will define your frame height.
IF YOU SO DESIRE, you should be able to zoom in using the properties below after you have done this.
[First Answer]
If I understand your question correctly, you may want to still perform your box frame manipulation, but if you wish to scale you may want to look into the
contentScaleFactor or
contentStretch
properties.
contentScaleFactor should scale both dimensions based upon a singular floating point value (i.e. xWidth * scaleFactor, yHeight * scale factor).
contentStretch is a CGRect which means that it should scale each dimension (axis) separately.

Confusing (x, y) origins with sub-views in interface builder

I have a view in interface builder, and then a subview within that view. I place the sub-view visually near the upper left corner within its parent. I would therefore expect the x, y coordinates of the sub-views frame to be something close to (20,20), but instead IB tells me it is something like (230,432). If I then adjust the X, Y coords in the properties window to something like (20,20), the sub-view flies off the screen towards the upper-left... What is the deal? BTW, it is an iPad specific view size, where the top/parent view is sized 768x1004.
In the Size & Position section, in the grid on the left, make sure the top-left corner dot is selected. The dot selected in this grid determines what the X and Y coordinates refer to.
You may have the center dot selected and so the X,Y coordinates indicate the coordinates of the sub-view's center relative to the parent.

Resources