UIButton changing width when label changes - ios

I'm writing a calculator app with a shift key. When the shift key is pressed, some of the UIButtons' labels should change. The problem is that when they do, the widths also change and, because of certain design constraints, other buttons will resize themselves too. How to I lock the width of a button (either pragmatically or in the storyboard builder) so that it will never change (regardless of the contents of the button)
Thanks for any help in advance.

Buttons, like labels have an intrinsic size that's set by the width of their text plus some padding. If you want them to have a fixed width, then in IB, just give each button an explicit width from the menu, Editor --> Pin --> Width.

Have you tried using [button sizeToFit]? Call it after you resize the subviews within and then you will likely want to apply some "padding" as this will size the view to exactly fit it's subviews.
UIView sizeToFit

Related

Add padding to sides of text in a button

Using Xcode's storyboard, how can I add padding to the left and right of a UIButton's text? I can't for the life of me figure out how to do this.
Here is my button:
I want the button to "hug" the text and then give padding of 32px on the left and right sides of the button.
But when I try to add a Title Inset, it just squeezes the text to a point where it's almost not showing:
So how do I adjust the button's padding properly?
Ensure following things are done for the Button in storyboard.
Update Content insets value for Left and Right as 32px in Size
Inspector in storyboard.
Also, ensure Content Compression Resistance priority -
Horizontal value is high (751) for safer side.
Remove if you have any width constraint for Button, if possible.
Do not update Title insets. It will not work as you expected.
I have tested this. It's working for me.

Using AutoLayout to grow a view based on dynamic text in subview

I'm working on an app that supports dynamic type. I have a simple UIView that is used as the footer in a table. Within that view is just a UIButton. When the user increases the dynamic type size, the text in the button's label grows, and I need the button itself, and the containing view to both grow appropriately. How can I achieve this?
Thanks,
The UIButton class has an intrinsic size, meaning that if you don't constrain it to a specific size it will size itself to a size that will fit its content. This also means that you can tie your button to the edges of a parent UIView and let the button control the size of the parent. This will happen automatically as long as you don't explicitly constrain the size of the UIButton or UIView.
If you want to, you can choose to set a constraint on one axis and have the button control the size of the other, for example let the button control the height of your footer, but always use a fixed width for the footer. In this case you add constraints to the width or left/right of your footer.
Read more on controls that supports intrinsic sizes
If you need space around the button label you can add it by increasing the content insets.

Why doesnt my textview show up when I add autolayout constraints?

I have a view which contains a textview (which displays a question) at the top. And 4 labels (for various answer options) with a stack of option buttons at the foot.
When leave the 'question' textview without constraints it appears fine in portrait. When I rotate the handset the text doesnt expand across the width of the screen.
I thought the way to resolve this was to click the add constraints pin/align button and click the T bars adding '0' constraints for top trailing and leading.
However when I do that the textview doesn't display at all. Any idea why?
The height constraint is missing. When you don't provide any constraints to your UITextView Xcode automatically will add the constraint's but as soon as you try to pin the top, leading and trailing anchors Xcode will not add any constraint's automatically.
You have to pin the bottom anchor of the UITextView to any of your buttons or give it a height constraint will solve your problem. Because UITextView will not calculate it's height based on it's inner content size like UILabel or UITextField.
If you want to only display some text and no edit functionality then you can use a UILabel and set the number of lines to 0.

How to change uiview's height using auto layout?

So I created this scenario in order to understand how views increase in height according to their content. However am still not able to make it happen.
This is what I have now:
the textview is growing according to content. however the uiview containing it is disappearing. what constraints should I use so that when the uitextview becomes bigger, its parent view also increase in height?
Start by clearing all your constraints from everything so we have a fresh slate.
Step 1: Build your view hierarchy. In this example, we want something like this:
The view controller's view, which we'll call parentView has a subview, which we'll call redView. That redView has a child, a Text Field, which we'll call textField.
The hierarchy looks like this:
Step 2: Set any constraints that are specific to any individual view. In this case, we probably only want to set a width constraint on our text view. For now, I'll just set a width of 200pts.
Step 3: Set the constraints between textView and its parent, redView. Let's say we want a 10pt border all the way around. Let's add these constraints:
Once we've added these constraints we'll gets some auto layout warnings and errors. For starters, because the constraints I added for with and space to superview don't match the actual sizes, I'll get some warnings like this:
There will also be some errors describing missing X and Y positions for redView and for textView. There are really twice as many errors here as necessary. textView knows where to position itself relative to redView. We don't need more constraints to sort out textView's position. However, redView doesn't know where to position itself yet... and so ultimately, textView also sort of doesn't exactly know.
We can update the textView's frame to get rid of the warnings, but let's go ahead and fix the actual errors.
Step 5: Set up redView's constraints relative to the superView. redView already know what size to be. Notice we had no errors for redView's width. It just doesn't know where to be. In this case, I'll go simple and say we want redView to be centered. So we'll want to add these constraints:
Now we've fixed some of the problems. The only problem that remains is the height for everything.
To fix this, we must set the content sizing priorities of textView. Set these all to 1000 and change the "Intrinsic Size" property to "Placeholder".
By now, all of the auto layout errors should be gone and we should only be left with warnings because our storyboard frames don't match what our constraints say they should.
We can fix that by selecting parentView and updating all the frames:
There's one final caveat to this auto layout puzzle when it comes to autosizing based on content size: what happens if our text view has no content?
If our textview has no content, auto layout will choose a height of 0, and our users won't even be able to see that there's a text view there, much less tap in it to add content (and make it expand). When using auto layout and content-based sizing, we should almost always be sure that we've set either an explicit or minimum size for the content view.
We don't have to worry about our textView's width, as we set this explicitly to 200. So let's add a minimum height constraint. Start by adding any height constraint:
Now go to the size inspector for the textView, find this height constraint we added, and edit it into a greater than or equal to constraint:
Storyboard won't reflect the change in content in our textView and resize it appropriately, but your constraints are now set up correctly and this will behave appropriately on your device or in the simulator.
ON UITextView make your you unselected These
Scrolling Enabled
Bounces
Bounce Horizontally
Bounce Vertically
Shows Horizontal Indicator
Shows vertical indicator
Now Change your auto layout constraints like this.
On the Storyboard Page, click on your textview.
Then click on the small triangular in the lower right corner.
Click first on "Clear Constraints".
An then on "Add Missing Constraints".
Its the easiest way.

Button autosize with auto layout in Xcode 6 w/ swift

could anyone of you give me a hint, how to implement autosize of a button with auto-layout?
To be more specific:
My button has 50x50px at iPhone 4s. How to implement this button, that its size increases on iPhone 6 plus screen resolution? Is there a kind of multiplier option?
Edit:
I want, that the button w/ 50x50px on iPhone4s screen is increasing proportionally at iPhone5, iPhone6 display.
Thanks.
BR
Use aspect Ratio. It's all about math. Before you jump into the action, let's figure out how we are calculating the proportion.
So, if you want your button to be 50X50 on a 400X600 frame, how will you find the proportion. Easy- your button's width will be 1/8th of your frame and your button's height will be 1/12th of your frame's height. That's it. Now you have your calculation.
Step By Step Instruction:
First make sure, you have the size class as "wCompact hAny". We should see, something like-
Now we are ready-
Select you ViewController and then select "Stimulated Size" in the inspector. There you will see your Stimulator's width and height. In my case, it is 400X600. You can change it to anything, and calculate your proportionality value accordingly.
Now, we need to position our button. I am just setting it at the middle of the frame. So, hold the "Ctrl" button in the keyboard, click and drag the mouse pointer from the button to the immidiate parent view which will give you a list of options.
From there, when you select the centre horizontally and the centre vertically option your button in the frame should look like this-
Now you can see some orange dots, but ignore that for now.
This is the part, where you will have your answer. To set the button size proportionally, you can use aspect ratio.
Again, hold the "Ctrl" button in the keyboard, click and drag the mouse pointer from the button to the immidiate parent view. This time select aspect ratio from the list.
Select the aspect ratio constrain from the constrain list to edit. You need to make sure that your first Item is Button's width and your second item is superview's width or vice versa. Remember, you want to set the button's width's proportionality value(multiplier) in accordance to the frame's width NOT height.
Now, set the multiplier to 1:8 if your 1st item is your button's width, or 8:1 if your 1st item is superview's width.
Do the 3rd step again. Select aspect ratio again, and select this constrain to edit. However, this time set your first Item as Button's height and your second item as superview's height or vice versa and change the multiplier to 1:12 or 12:1 accordingly.
Great, if you have followed unto this point then you are done. You should see something like above.
You need following three constraint
For X position : Leading Space to SuperView
For Y position : Align Center to Y
For Width : Trailing Space to SuperView
Check ScreenShow

Resources