How to set AutoLayout constraint of UIimageView with UIButton? - ios

This tree include the cicle is a UIImageView's image, but the text in the circle is a UIButton. When the device is changed, like iPhone5 change to iPhone6 Plus, the image with scale to big. The Button always move to the circle's outSide. How to Solve the problem?

Autolayout has a feature called propotion. It means that by selecting any 2 UI objects you can set propotion between them for any constraint such as width, height, etc.. In tour case also select imageview and button then move to the constraints view, double click on any objects width constraint i.e either button width or image width. There you can find a option called propotion. As per your requirement you want to have a button always inside the image view, means that buttom propotion should be less than the imageview propotion. So you can set 2:3, means button width is 2/3rd part of the imageview. Now button will be always part of the imageview irrespective of the device which you run.

You can Add proportional width and height constraints to your buttons relative to imageView like this screenshot
Here I have not added fixed height and width constraints to imageview, because image view takes width and height from its content i.e from image provided to it, if you want to fix height and width of image view then you can provide those too.

Related

Rescale UIImageView proprtionally in Launch Screen Storyboard

I have a UIImageView that needs to be centered and have a width that's equal to the screen width and some height that grows proportionally with the width. So for instance, for the iPhoneXR I have:
And on the iPad 9.7 it would be:
So basically the image just scales proportionally with each screen size. I have already figured out how to center it using constraints, but I'm lost on how to get it to scale proportionally. I've played around with the auto resizing options and constraints to no avail. I struggle so badly with these storyboards, wish I could just code it! Is this even possible in a LaunchScreen storyboard?
You can't execute any code inside launch screen. However, you can make it with constraints.
Set center equal to superview, then set width equal to superview. Now, you can add aspectRatio which is like 1:1, so you imageView height would always be equal to its width or any other ratio you want.
Example:
By adding proper constraint you will get your expected result. Add aspectRatio constraint to imageView.
All constraints as per
You can not give constraints to a view w.r.t viewController in LaunchScreen.storyboard because our view hierarchy has not loaded till that point of time.
You can give an imageView constraints w.r.t itself. CTRL drag on the imageView and select height, width or aspect ratio.
Though the recommended way will be to use another viewController that will work as LaunchScreen with some delay in segue.

iOS Stackview inside ScrollView with preserve image aspect ratio

I'm having a problem with scrollable content. Basically, I want to place one ImageView and one Label on the screen with ImageView width equals screen width. height depends on intrinsic content aspect ratio (while width is always equal screen width).
The label is 20 spacing bellow ImageView. That means whole content height may less or equal or even longer than the root view. What I have tried is to put Label and ImageView inside a StackView (ImageView has Aspectfit setting), Then put StackView inside ScrollView. Set up constraints as below.
The problem is the distance between ImageView and top Superview and between ImageView and Label is too big (ideally should be 20). Is there any way to achieve the desired outcome?
PS: I have tried to set stackView distributtion but no help
tl;dr Remove the aspect ratio constraint and add a height constraint for the UIImageView.
It’s important to understand that UIImageView is basically a container that holds an image, and its dimensions don’t necessarily reflect the image’s dimensions (unless you manually make them equal). When you selected Aspect Fit as UIImageView’s Content Mode, the size of the container didn’t change to correspond the image’s size; this setting only changed the way the image is placed inside the container.
As I can see in your screenshot, the UIImageView’s height is greater than its width; furthermore, the aspect ratio is fixed by the constraint you’ve added. When you placed a landscape-oriented image inside this container, the latter left white bars on the top and the bottom of the image (just like the black bars you see when you watch a widescreen video on an old monitor). Change Content Mode to Aspect Fill or Scale to Fill to see the actual size of the UIImageView.
To fix this, remove the aspect ratio constraint and set up a fixed height for either the UIImageView or the UIStackView. If I were you, I’d probably set the UIStackView’s height equal to Safe Area so that no matter how tall the stack view is, it doesn’t go beyond the screen boundaries.

Constraints not resizing properly for all screen devices

I am trying to do the constraints for these horizontals button. I want the ratio of size of buttons to be the same, and the icons to be of equal widths and heights of each others.
Any idea of how i can do that so these buttons resize properly according to the screen size? Thanks!
Make groups of UIView containing the icon and text. Lets call this container view
Place all the n container views inside your storyboard as you would like them to appear. Now:
To the left most container view add a leading and bottom constraint to the super view.
Now to the second container view add a leading space of 0 (or anything you want). Control + drag your second button to the first button. Hold down shift and select equal width, equal height and align bottom.
Now apply the same constraints as your second container view to all your n - 1 container view. n being the number of container view you want to add. Now to your last (nth) container view add, one extra constraint, which would be a trailing space to the superView. Now all your container view ought to have an equal width that will be determined depending on the width of the screen!
If you want to have a specific height or aspect ratio to all your container view. Just add the height or aspect ratio constraint to your first container view and all your subsequent views will get updated accordingly.
OR
If you wish for the height to be dependent on the screen size and not maintain a specific aspect ratio, then you will have to give the first container view a equal height to the whole view with a specific multiplier like 0.15.
You will also have to add appropriate constraints to the icon and label present inside each UIView
Edit: A much easier thing for you to do would be add the icon as an image to the UIButton and add the text as you would normally to the UIButton. The UIButton will appear quite similar to the screenshot you posted. And then just apply the constraints I mentioned above.
Set the width and height as ratio of the SuperView. Set if for one button and for the remaining buttons make the height and width equal to the first button for which you defined the height and width in terms of superview height and size. Use this SO Post to see how to set height and width as ratio of the superview.
Hope this helps.
It's Simple because your All buttons are in single Direction so you can use StackView.
Just simple first apply the equal hight and equal width to all your buttons
now select all the buttons and add them in the stackview
it will be in the right side bottom. (with the constraint icon)
now simple Apply add missing constraint. it will done the work by own and gives better result. (but take care here apply it from the all views in View Controller Section)
And now Bingo try this every Size will show same.
This will work same in simulator also.
The solution is very simple.
See the image below (5 buttons)
The first (blue) button is pinned to the left and bottom of the superview
Each of the other 4 buttons (red, black, green, pink) are top aligned to the first (blue) button
Each button is using a horizontal spacing to the previous button (with a constant of 0). So red button has 0 horizontal spacing to blue, black has 0 horizontal spacing to red, etc
The Last (pink) button is also pinned to the right of the superview
Finally all 4 other buttons are set to have same width to the first (blue) button
That's it!
As for you icons, all you need is to set them to have same width & height to the first icon you have

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

How to scale a UIImage view to a square with Autolayout inside a UIScrollView

I've been struggling to learn autolayout (finally). I want to have a vertically scrolling UIScrollView that holds all the content for the view. I want to pin a UIImage view to the top of the scrollview and keep the image a square (scaleToFill). Basically I want to do this (only I want to do it with autolayout):
I can't get the image to keep its aspect ratio while staying within the screen bounds. Whenever I add an aspect ratio constraint the imageView grows to like 600 points (the width of the png I think), but I want it to just be as wide as the screen.
I think I am setting the constraints up for the UIImageView correctly because if I get rid of the scrollView and just drop the imageViw directly on the view then it seems to do what I want it to. The trick is putting it inside the scrollView.
This is how I currently have it set up:
The 4 vertical/horizontal space constraints on the scroll view are all set to constant 0.
Since the size of the UIScrollView depends on its content, you cannot have the size of your UIImageView subview dependent on it.
You have to remove the aspect ratio constraint of the UIImageView and use a fixed width one. You can also have your UIImageView width dependent on another view in the UIScrollView which has either a fixed width or otherwise unambiguous width.
Don't forget to use Placeholder as the intrinsic size value in Interface Builder.
Hope this helps.

Resources