Resize button in swift/xcode with Autolayout enabled - ios

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.

Related

Right-alignment constraints?

I'm trying to add constraints to right-align a button to its parent view, but having some problems. In the following image, I've created left-aligned constraints for the < button. The Play button is center aligned, independent of the < button.
I've tried adding a constraint for the Reload button to be right aligned, but it sets a fixed distance which doesn't align but rather creates a fixed position. Not sure what I'm missing here.
I've updated this question with an image of the existing constraints.
Have you been using "Trailing Space to Container"? If this doesn't work you will need to check:
Which view is the container of your Reload button to ensure it isn't aligning with the wrong view
That the container view has the bounds you expect it to have
If you need further help, could you please upload your view hierarchy, like so:
Also the existing contraints on the reload button would be helpful so we can eliminate any interaction between constraints :)

How to set up my constraint perfectly to the bottom in Storyboard

I am using AutoLayout in Storyboard. I have set translateAutoResizingMasksToConstraints to false. I have created a constraint like so:
The superView takes up the whole screen's frame. When I run the App, the bottom bar view doesn't appear. However, if in the picture I change constant to 50, it jumps up to about where it should be expected.
I have had a previous branch where I did not set it to 50 but due to some other constraints it appeared as expected. But it's not exact and I don't know why it's behaving this way all of a sudden. I've used the View Debugger feature and the bottom bar view straight up doesn't appear at all.
How can I set it so that the constraint has a constant of 0 and still appears? What might be a reason for this bug? Thanks!
When you are trying to add constraints to two items, you try an easier way by choosing two of them(even super view) like this:
Then click 'add new constraints' on the right bottom of storyboard/xib,
Then you can choose different types of align constraints as you like.
If you only choose one item to add new constraint, then align will be disabled cause it needs two items.
Try printing the bar's frame in view did appear, if you are not sure whether the bar is just outside the visible field. Have you set constraints to the sides and a height as well? As a rule of thumb you can say that a view needs 4 constraints before iOS knows exactly where you meant for the view to be placed.

Misaligned custom navigation view on iPhone 5 & 4

I'm having an issue where my custom navigation view misaligns when the left and right buttons are different sizes. I'm using size classes and the constraints I've set are shown below:
This method has worked extremely well for me in the past but my designer requires that the right button be a rounded "save" instead of simple text. When viewed on iPhone 5 or 4 devices (narrow width) it misaligns and gets pushed to the left. Here is an example of how this looks on the device and in View Debugger.
I'm trying to avoid redoing all of my button images to be the same width and hoping there is a simple IB fix that I can add.
Any help would be greatly appreciated.
you can add 'Horizonatally in Container' constrain to Navigation Bar Title View.
Thenceforth you can add trailing edges constrain(with or without margin) between save button and its superview.
Similarly you can add leading edges constrain between back button and its superview. Take care that superview of all 3 views should be same. Hope it helps. Happy Coding. !!
*EDITED
You can refer my ans here and for customisation here.

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

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.

Why do autolayout constraints in Xcode cause my subview to disappear off screen?

I'm trying to figure out how to use autolayout, and there seems to be a lack of info about it on the internet. I placed a view within my view controller with three buttons inside it. Before I place any constraints, the buttons show up on the preview. However, as soon as I make ANY constraints, the whole sub view completely stops showing up in the preview. I made a constraint to center vertically, and then one to place it 25 pixels from the left edge. It should know exactly where to place it, but still nothing shows up.
Any ideas on why constraints cause my buttons to not show up?
With Autolayouts the constraints should be as clear as possible. You can try by adding size constraints as well as fix(right, top or bottom) constraints if necessary.
When you don't use auto-layout it positions everything manually. When you enable auto-layout it switches this behaviour off so you have an unconstrained object, which is why it dissappeared.
Turns out I didn't have constraints on the size of the sub view, so it didn't know how to center it. I didn't think I needed that because I thought it would default to what it's already at.

Resources