Fix objects inside subview into place - ios

How can I fix objects inside a view to a position? What I mean is: I have a subview which includes multiple elements for example an image and a textfield. Those subviews mustn't change their aspect ratio. I know I can set that via auto-layout. But how can I set that all the elements in the subview stick relative to their position inside of that subview? So that if I resize the subview (while remaining in their aspect ratio) the content of it will look the same just up- or downscaled?
Thank you!
UPDATE: This is how it looks like. The white area in the middle is the 'subview', and the textfield together with the labels the 'content':
Now if the screen size of the device changes it looks like that:
I have a constraint saying that the 'subview' should keep its aspect ratio. And I just want the 'content' to resize accordingly.
If I try to auto-layout the textfields and labels to 'subview' I get many weird errors. Isn't there any easier method? I mean it's just resizing and not some complex UI changes.

I haven't tried but, if you are scaling your view then inner views are will scale in ratio of the outer view.
But if your plan is to stretch outer view from the corners may be this layout setting can be help full.

Inside the storyboard you have an option to give something the same height or width as the parentView if you ctrl + pull to it.
Then it will say x equal to parent y = 1 whewre 1 is 100% of size, size it down to for example 95% and the align the views in the center, left or right or wherever you would like.

Related

How do I make two equally sized imageviews cover the whole screen

I implemented a viewcontroller similar to the one in this question:
iOS two views cover exactly half of parent view
I get the desired result. The problem is that when I add a UIImageView into those two containers, the result get resized and ruins the symmetry. How do I prevent Imageviews from changing the size of their containers? I need to use AspectFill for these images.
I don't think the containers' sizes are changed. You just need to set their clipsToBounds property to true to avoid overflow.
If you use Reveal you should see the containers' sizes are not changed even if pictures inside them are bigger than themselves.
I'm unsure I understand what you mean about adding UIImageView into those controllers causing the views to resize, so forgive me if I'm getting this all wrong...
It sounds as though you've created two views of equal height that, together, consume the entire vertical space of the screen. After having done that, you want to add one or more UIImageView's to each of the original two views. Unfortunately, when you add the UIImageView, the enclosing view is resized.
Assuming I have that correct...
Are you doing this with Interface Builder either in an XIB or Storyboard file? If so, you ought to be able to achieve this with the proper set of constraints.
In the following image, I've laid out what I describe above.
As you can see, I have a red view on the top half of the window and a green view on the bottom half. The red view contains a UIImageView that is 75% of the width and height of red view, with its origin at (20, 20) within the red view.
The scene is configured as shown below:
The constraints on "Upper View" are:
You can see from this that Upper View is flush with the left, right, and top of its superview, and that its space to Bottom View is 0. You'll have to trust me that Bottom View is set up the same way.
The height of the Upper and Lower views is "Proportional" as shown in this constraint:
To achieve this "Proportional" setting, you first make the height of Upper View equal to the height of the superview, and then edit the constraint, changing "Multiplier" from "1" to "0.5."
The height (and width) of the Image View is proportional to that of the Upper view, as shown here:
If you set it up this way, you ought to be able to accomplish what (I think) you are looking to accomplish.
If my original assumption of what you are trying to achieve is incorrect, please post images of what you've got and how it's not working.

Xcode Constraining UILabel vertically over a dynamic UIImageView

I am trying to constrain a label that is sitting on top a UIImageView. The ImageView keeps its aspect ratio for the different screen sizes so its length and width change according to the device.
I am running into an issue if I constrain the top of the label to the top of the image view at (for example) a constant of 58. For the lower resolution image that places the label where I want it visually. However on the higher resolution images that position is not where I want it visually. I have also tried adjusting the top constraint so that the constant is 0 and use the multiplier to adjust the position of the label. This however does not fix the problem and the label ends up at different locations on the image.
I really would not like to have to edit these constraints programmatically as I will have way more labels on different view controllers that would be a pain to program. Really hoping I can achieve this in IB.
I'm confused... why aren't you pinning a constant distance from the top of the label to the bottom of the image view?

Pixel Perfect UILabel

Here are two labels stacked directly on top of each other, with their backgrounds colored:
I always design my apps in Photoshop first. That dead space on the top and bottom of the labels makes it extremely difficult to position text precisely as in the design. How can I compensate for that top and bottom space when I want to place the label by top or bottom of the text?
Try calling -sizeToFit on the label in order to resize it.
Call this method when you want to resize the current view so that it
uses the most appropriate amount of space. Specific UIKit views resize
themselves according to their own internal needs. In some cases, if a
view does not have a superview, it may size itself to the screen
bounds. Thus, if you want a given view to size itself to its parent
view, you should add it to the parent view before calling this method.
Either use AutoLayout to size your labels exactly at the content size, or use sizeToFit in code and position your labels accordingly.
Can you not create a background UIView with the background colour, and then add the label as a subview of the background view?

Center 4 images with constraints autolayout

I'm new to autolayout and i'm kind of stuck on how to center these 4 images in all different devices like it looks on the images. i've tried to apply the auto configured constraints but then it will have that distance and that does not fit on all devices. So my question is what constraints do i need to apply on all different images in order to make all image centered with same distance?
Here is how my cell in storyBoard looks like:
image of the constraint options on image 1:
image in simulator:
/// MY TRY ////
Here you can see the constraints that i've added and the result?
result:
Your approach is almost correct, it simply lacks size constraints for the images.
If you want to dynamically resize the images and keep the space between them constant, put constraints on the images for the width to be >=20(or any other value, depends on your needs) and a constraint for keeping the aspect ratio. Then ctrl-drag from UIImageView1 to UIImageView2 and set a constraint for equal widths. Repeat that from UIImageView1 to UIImageView3 and from UIImageView1 to UIImageView4.
If you want the images to always keep their fixed size and dynamically sized spaces between them you need another approach:
The trick here is to place 3 empty UIViews between the UIImageViews so that it looks like this:
Set constraints to the top for all views
Set constraints to the left and right for UIImageView1 and 4
Set constraints for all UIImageViews to the same width
Set constraints for all UIViews between the UIImageViews to the same width
Set constraints for all the distances between the views to 0
Set the width constraints for the UIViews to >= 0
This way makes the empty UIViews between the UIImageViews to resize dynamically and all the same width.
I hope you get the idea.
For the first image add a leading space to superview and for the last image add a trailing space to superview and add vertical spacing between first and second, second and third, third and fourth and also set horizontally center within the container to each of the images
The objects on your storyboard require at least 4 constraints per object.
When there's orange lines around the UIImageView object like appears in your question, this means a constraint is missing, or conflicting and can often mean the image does not display at runtime like you'd expect.
In the first instance I recommend you select all objects and 'clear constraints', then start again.
Although not exactly the same, here is a link to an answer which explains the auto-layout constraints in a bit more detail. Once you understand it, auto-layout can be very powerful.
>>Link to similar auto-layout issue - iOS CustomTableViewCell: elements not aligning correctly<<
I hope this helps
If you want the images to always keep their fixed size and dynamically sized spaces between them then very easiest way first you drag the four UIView in your StoryBoard
set constrain top left right and height for all the UIViews and give the equally width to all the UIViews(drag from one UIView to another and sets its equally width repeat this work for all the UIViews).
Image about layouts
Secondly you have to drag your images set in these UIViews and sets their constrain height width and centre horizontal in container and centre vertically in container.
In last set the background colour of all UIViews default(white) you get the result it will work....i assure.
Screenshot of Xcode

Autoarrange subviews in a zoomed UIView

I'm trying to zoom in and out an UIView, and rearrange it content to look similar for both states: zoomed and normal.
This picture shows the default state (the view that I'm going to zoom has orange color and has 5 UIImageViews) :
When I press "Zoom in" button I change orange view frame:
_page.frame = self.view.bounds;
And I'm getting the following result:
But the goal that I want to achieve is something similar to this (same result if I would scale the view):
It means that I must change frames for each subview, but it could be complicated when view would have many objects on it.
What I'm asking for are some hints or methods how can I get desired result without accessing subviews.
There are be hacks to do this, but the proper way would be to use auto layout. You don't have to access any subviews and will be able to do it in the storyboard/IB.
If you use auto layout, you can actually create constraints which will pin the following attributes of the subviews:
Pin the top subview's top space and leading space to the container
Pin the all but the last subviews' vertical distance to its nearest neighbour and leading space to container
Pin the last subview's top vertical space to its nearest neighbour and bottom space to container and leading space to container
Set constraints for height and width but set the priority to low
In addition to setting the frame (which just changes the size of the view) you want to change the transform (scale the view) Try something like:
_page.transform = CGAffineTransformMakeScale(2.0, 2.0)
You'll probably want to calculate the scale factor based on the old view size and the new size.

Resources