Align edge of element to center of superview in xCode/swift - ios

I found another stackoverflow post asking about essentially the exact same thing I am trying to solve (iOS Autolayout how to stretch 2 button horizontally), however I am still having issues figuring out how to go about aligning the edge of my object to the center of the superview. I need my two buttons to be aligned side-by-side just as in the example in the post I linked, but I cannot figure out how to reference the center of the superview to set my alignment to it, like is shown in the screenshots. Could someone provide a little more insight as to how to achieve the result mentioned in the linked post? Thank you so much.

One way is to set the pin the left button to the left edge. Right button to the right edge, Pin the left side of the right button to the right side of the left button. Then set their widths equal. This is the easiest way for me if I'm using storyboard.
Make sure the constraint between the two buttons has a constant of 0 or however much space you want between them.
If you take a look at this screenshot it will click that button when its red or yellow it will tell you what constraints you are missing on a button.
If a view is misplaced simply hit the view and press cmd+option+"=" and it will give you a preview based on the constraints you've placed on it.

Related

Xcode constraints is pushing everything on the view to the very top (behind navigation bar)

I'm hoping someone can help me out here. I set where I want the items to be using constraints and when I run and build the app it seems to push everything to the top. This occurs even when I don't put any constraints on any items. I've attached pictures if they help at all.
Have you tried setting the Top Constraint of your topmost subview to "Top Layout Guide" as seen in this screenshot? (Looks like your Top constraint is to the 'Superview' instead!)
http://i.stack.imgur.com/fpHAQ.png
Making sure your view is selected go click on the triangle constraint button in the bottom right and select clear constraints under all views.
Place the views where you want them. Then for each element click the square constraint button on the bottom right and do this:
Uncheck constrain to margins
Pin to top bottom and both sides
Check width and height
Then select the triangle and choose update frames for selected view.
Fine tune position by by selecting the view you want to move and updating the constraint value.
Because sometimes its just easier to start again than to try and fix constraints.
I've figured out a solution. Somal was on the right track in terms of the Top Layout Guide. I simply had to right click each individual object to drag onto the view and select Vertical Spacing to Top Layout Guide (See picture) or I could of simply delete the entire ViewController and start from scratch with no copying and pasting from the old View.
VerticalSpacingMenu-Image

Resize button in swift/xcode with Autolayout enabled

I have read in several other SO posts about this topic, and it's my understanding that to resize an object when AutoLayout is enabled, you can simply change the constant properties of their constraints in your code.
I have two buttons side by side, like this
[delete][ post ]
and they take up the entire width of the screen. I want to hide the delete button for certain views, and when that happens, stretch the post button to take up the entire width of the screen. In my code, I have:
self.postButtonLeadingConstraint.constant = self.deleteButtonLeadingConstraint.constant
self.deleteButton.hidden
My delete button hides just fine, but my post button stays exactly where it is at. What is it that I am doing wrong? Any input is greatly appreciated.
EDIT: If it matters, my buttons are constrained as follows:
Delete button leading constrained to leading edge of view
Post button leading constrained to trailing edge of delete button
Delete and post button set to equal each others' widths
both buttons have their tops and buttons constrained to the view they are in, but I don't see how those could be relevant.
If you're targeting iOS 9 and higher then I'd suggest using UIStackView. This is super easy that way, just a matter of adding and/or removing the button from/to stack view. Here's little sample to help you understand.
Try this:
self.postButtonLeadingConstraint.constant -= self.deleteButton.frame.size.width
self.deleteButton.hidden = true
The problem is that although you are hiding the button, it's width remains unchanged. If you use
self.deleteButtonWidthConstraint.constant = 0
Where deleteButtonWidthConstraint is an #IBOutlet hooked up to the delete button's width constraint.

Size-classes and constraints with buttons

I hope someone can help here as I've been reading and watching a lot about size classes as I've just started work on first app using size classes.I thought I'd got the idea but having a slight issue.
I've got a view with 10 buttons in it. I want the buttons to re-size to fill the desired screen but as you can see from the screenshot below all I'm left with is loads of white space on the bigger views?
I've set constraints to the view and to each other so I though Aspect Ratio should solve it....No, all I get them is one huge buttons and the rest stay the same!
I've researched a lot so any guidance would be greatly appreciated.
Please see this image of the issue.
Thanks
Pin the top of the top button to its superview.
Pin the bottom of the bottom button to its superview.
Assign a fixed vertical space between each button.
Select all the buttons and specify "Equal Height."
This should take care of your vertical constraints, and the button heights will equally expand or contract, depending on the screen height.

Two Buttons. Above each other. Over whole screen

I just like two buttons above each other with no margins. This seems like an easy task, but I am new to iOS and I wasted too much time now. I tried dozens of contraints but it never went out well. I always ended up with setting the leading Spaces to -24 and having marings above and beneath the buttons. I am using xcode6, iOS8.1 and storyboard. My app is landscape only. Can someone please teach me what to do?
The buttons should look like seen in this picture: Fullscreen, no margins and Button1 should reach unter the statusbar!
I'll outline the steps:
Pin Button 1 to 0 spacing on Left, Right, Top
Pin Button 2 to 0 spacing on Left, Right, Bottom
Select both buttons, then Pin heights equally
(You can access pin either from the Editor tool bar in the top menu, or from the second icon in the constraints area)
The thing with constraints and auto layout is that you only need enough constraints to let the app calculate where elements should be at all times. You might be able to get away with less constraints.
To get remove the borders, you need to change the button Type in IB from System to Custom, then you can drag and alter the margins to remove them. Following that you will need to update constraints on the buttons.

Set fixed space between 2 views in Interface Builder

I am trying to learn to use layout constraints in interface builder but I am running across an issue...
I've got two views, one UILabel and one UIImageView, and I'd like a layout as follows:
the label should be centered in the superview
the imageview should be a set distance to the left of the label
Seems pretty simple, but I can't figure out how to impose a constraint for the second condition (the first is done automatically). Anyone know how to do this?
Thanks!
Click on both views while holding the command key so they're both highlighted. Then click on the pin button in the bottom right hand corner of IB (the middle button). Select horizontal spacing and the constraint should now be added. You should be able to adjust it as needed from there.

Resources