Autolayout - Modify center align with multiplier - swift

I want to have to buttons, which are horizontally centered in the container. Because they would overlap I want them to be a little offset from the center. I tried it with the multiplier.
If the button is in the center (as normal) the multiplier is 1. So I thought that for the left button, I specify a multiplier of 1,2 and for the right button 0,8.
And also, why is the constraint of the left button moved an the other one not?

I can't speak to the behavior of the multiplier for the center align constraint. If I were doing this, I would:
Create a view to hold both buttons. Drag both buttons into the new view.
Pin the top, left, and bottom of TOR to the top, left, and bottom of the new view, all with constants of 0.
Pin the top, right, and bottom of STRAFE to the top, right, and bottom of the new view, all with constants of 0.
Pin TOR to STRAFE with a horizontal spacing of 20. Fix any misplaced views by updating their frames.
Make the new view's color clear.
Now you can click between the buttons and they will stay together. Center their containing view horizontally and give it a vertical constraint and you're done.

Related

Unity stretch scrollview bottom to the bottom of the screen

I would like to stretch the scrollview's bottom to the bottom of the Screen.
I tried with
scrollViewItemRt = (RectTransform)scrollViewItem.transform;
scrollViewItemRt.sizeDelta = new Vector2(0, -Screen.height);
But it doesn't work.
First, make sure your ScrollView is a child of a Canvas which covers the screen, and not a child of any sub-object that only covers part of the screen.
In the ScrollView's RectTransform, pick one of the options that makes it stretch vertically:
such as the one highlighted. The ones in the bottom row here, with the blue ↕ character, will all work.
Then, the PosY and Height options will change to Top and Bottom. Make sure they are both 0, or small numbers if you want a little padding.

Center Label vertically between two UIButton's

everybody some I'm having some trouble vertically centering the or label between the Sign in with apple button and also the login button. Keep in mind that I am creating the SIWA button programmatically and setting the constraints that way also. I have tried getting the origin y coordinate for both buttons, dividing them by 2 and then setting the vertical constraint from the login button to the or label to be (loginButton.frame.origin.y - (divided by 2 value)) but that doesn't seem to work. Thanks for your help.
There's a couple ways to solve this:
Put the 2 buttons and the label in a vertical UIStackview with distribution set to equal spacing. Stackviews are very flexible too, especially in more complex layouts.
Create an empty UIView that sits between the two buttons, then add the label as a subview and center it. Alternatively, you can create 2 spacer views with equal height constraints above and below the label:
V:|[Button][space][Label][spacer2(==spacer1)][Button2]|
Very easy approach: by default, UILabel centers its text vertically in its frame.
So,
constrain the Top of the Label to the Bottom of the top button
constrain the Bottom of the Label to the Top of the bottom button
All done :)

Unable to pin button towrads right corner in tableView Cell

I tried many ways to pin the button towards right corner of the table view cell.It's fits properly only for one device segment for iPhone SE/5s button is not visible.
I had given equal width & height alogn with top and right constraints.
Below is the screen shot of auto layout segment.

fill rest of view in storyboard

How do i achieve the following layout within a storyboard? I have seen related answers in SO suggesting a code solution. But can it be done codeless?
For your toolbar at the top set the left, right, and top spacing constraints with a constant of 0, then set a height constraint with a constant of whatever your fixed height is.
For the bottom container view, set the left, right, and bottom spacing constraints with a constant of 0, and set a vertical spacing constraint to the toolbar, also with a constant of 0.
This will anchor the toolbar to the top and automatically fill the rest of your view controller with the container view.

AutoLayout: Vertically centering two UIViews in a superview that can resize

I have two UIButtons, one on top of the other, in a superview whose height can be resized. The two buttons should have a constant vertical spacing between them, but the top and bottom spacing should resize so that the two buttons stay centered in the superview as it resizes.
I tried creating two less-than-or-equal constraints (with equal priority) on the spacing to the superview for each button, as well as a constant vertical spacing between the buttons, as shown below:
(The reason why it's less-than-or-equal here is because this view is defined at the given height in IB for 4" screens, but can be shrunk for 3.5" screens.) However, this doesn't do the trick, as you can see from the screenshot while the app is running:
It's almost as if you want to be able to tell AutoLayout that the two constraints themselves should have equal values, even if they are both set to "less-than-or-equal". Is there any way to do what I'm trying to do, or perhaps a better way?
This is so trivial to do in IB.
1) ⌃ drag from button1 to top. select "center horizontally in container".
2) ⌃ drag from button1 to left. select "center vertically in container".
3) do the same with button2.
4) now the only thing left to do is size the buttons because this is what it looks like.
This is also very trivial.
5) ⌃ drag from button1 to the left. select "leading space to container margin".
6) ⌃ drag from button1 to the right. select "trailing space to container margin".
7) do the exact same thing with button2.
The finished product, looks like this (NB I didn't quite center them, but I could have easily enough):
The simplest way to vertically center is to add a NSLayoutAttributeCenterY constraint - preferably to the element that is near the center of the view. And if all views have a vertical spacing constraints, then they will all be centred. No need to muck with the view hierarchy or add spacer views.
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:button2
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
If you need to adjust the positioning, set the constant. For example: constant:-30 will move it up 30 points.
You can also anchor elements based on different logical areas of the view. For example, if you wanted to anchor your first button at 25% of the view height, you can set the multiplier to 0.5.
Agreed with rdelmar. Here's another option if you want to preserve view hierarchy.
You are currently spacing the buttons at the top and bottom using constraints. Instead, create two empty UIViews, they will be used as spacers. They should be positioned one at the top and one at the bottom of your buttons. Using autolayout constraints, make sure that the height of these two spacer views is always equal. Make sure they are pinned to the top and bottom of the buttons and the top and bottom of the superview, respectively.
In VFL: V:|-[spacer1(==spacer2)]-[button1]-(20)-[button2]-[spacer2(==spacer1)]-|.
You may have to do this in code, I'm not sure if IB can do this.
One way to accomplish this is to enclose the two buttons in another UIView, and center that view in the controller's view. Give the buttons a fixed distance to the top and bottom of this view, and either a fixed distance between them, or a fixed height for the view.