Full Screenbackground Image in Swift - ios

I currently try to get a fullscreen background for my iOS Project this means the image should stretch over the full background.
I currently try 2 things but won't worked out as solution.
1)
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
This only set the background image on the actual image size and repeat it in x and y, it does not stretch the image to fullscreensize.
2)
use a UIImage and make it equal to width and height and use aspect fill, this also does not give the wanted solution and also use much memory
Is there any other way to get this done?

So you have two ways to do it always with AutoLayout
1) With an addition of an Inequality constraint like shown in the images below.
2) Or by adding Leading, Trailing space and Top, bottom space Constraint and centering vertically and horizontally constraints but NO explicit height nor width constraint.

Related

Self-sizing tableview cell with only imageView [duplicate]

This question already has answers here:
Auto-Layout: Get UIImageView height to calculate cell height correctly
(5 answers)
Closed 4 years ago.
I'm struggling with the following problem:
I have a TableViewController in which every cell has only an imageView. It uses autolayout to cell's contentView's margins and it is set to aspectFit.
What I want is the height of cell to size accordingly to the imageView's height.
In the first screenshot you see the white spaces at each side of the first image and at the top (and also the bottom - second screenshot) of the second image.
Note: my images' aspect ratio is variable.
I've already set this with no luck:
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 300
Also this in cellForRowAt:
cell.setNeedsLayout()
cell.layoutIfNeeded()
Any advice is well received.
Thank you.
Images with aspect fill here
The constraints
You need to create these constraints , aspect ratio here is 3:1 you can change it according to yours , also the real image should have same aspect to stretch completely as you set contentMode to aspectFit
Edit:
If the aspect is variable then you need these constraints
then hook the height constraint and do this in cellForRowAt after you download / get from cache
cell.imageHeight.constant = imageRealHeight * imageCellWidth / imageRealWidth
This is due to the way the image view returns it's intrinsicContentSize. I currently believe this is a bug and should be changed. I have raised a radar for this.
When you put an image into an image view with .scaleAspectFit and with a constraint on one dimension (width or height).
Then it will scale the image down.
So if you start with an image of 300x300 and put it into an image view with a width constraint of 100 then you will get an image view that is 100 points wide.
However, the image view uses the unscaled image for the intrinsicContentSize and so it will want to be 300 points high.
This is the problem you are facing now.
The only fix for this currently is to calculate the aspect ratio of the image and manually set that as a constraint on the image view.
So you might have a line like this...
imageView.image = theImage
You can add to that to add an aspect ratio constraint...
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: image.size.height / image.size.width).isActive = true
Of course, you will also need to remove any constraints added already. Or possibly create the constraint as a property of the cell etc...

How to use Autolayout to achieve result as per shown screen

I'm developing an app where I'm facing an issue to achieve one UI layout only for iPhone devices. This is the layout that I'd like to achieve:
I want to achieve overlapped views that layout should be same on every device from 5s to 7 plus.
The key is setting proportional constraints.
Start with adding a UIView to hold the 4 elements - background, man, woman and heart.
Add a UIImage for the background, and pin the edges to the edges of the view - that's the easy part.
Add a UIImage for the Man...
Set the Aspect Ratio to 1:1 to keep it square (well, round in this case).
Set the Height constraint equal to the Height of the "containing" view, but then set the multiplier to less than 1 to make it relative to the view. In this case, 0.6 is pretty close.
Set constraints for Centered Horizontally and Vertically... then set the Multipliers to keep the image left and above the centers. 0.64 on horizontal, and 0.9 on vertical work pretty good.
Add a UIImage for the Woman...
Set the Height and Width constraints equal to the Man image.
Set constraints for Centered Horizontally and Vertically... then set the Multipliers to keep the image right and below the centers. 1.4 on horizontal, and 1.2 on vertical work pretty good.
Add a UIImage for the Heart...
Set the Aspect Ratio to 1:1 (or whatever gives you the proper ratio for your heart image).
Set the Height constraint equal to the Height of the "containing" view, but then set the multiplier to less than 1 to make it relative to the view. In this case, 0.15 is pretty close.
Set constraints for Centered Horizontally and Vertically... then it will need a little adjustment o note Vertical 1.05 worked for me.
Now, you just need to set appropriate constraints for the "containing" view, and all the elements will scale and position themselves within it.
When you're all done, it should (hopefully) look like this:
I put the project up in a GitHub repo for you to look at: https://github.com/DonMag/AnotherLayoutExample

What constraints should be given to increase size of imageview with respect to device screen size without reducing image clarity

I have to show an imageview in a universal app ans it's size should increase as the screen design in every devices without reducing image quality. The image given to the imageview is a static image. If I use static width and height constraints using size classes it will work. But I don't think it as the correct way. If i use width & height
Proportional to the superview, it increases the image height but image clarity reduced in ipads. Any suggestion will be helpfull.
To make Imageviewsize respect to device for universal application , you need to give correct multipler to imageview.
please follow step to get desire output,
Step 1 :- Set Aspect Ratio to your imageview . (you can also set leading and trialing top , bottom etc. and then give aspect ratio.)
Aspect ration required to maintain to set image equal width and Height (Square)
Step 2 :- Set Horizontal center and vertically center imageview . (it will be differ according to your requirement , you can also take this step as step 1 and then set aspect ratio)
Step 3 :- Set Equal height to Superview from imageview.
(Don't panic , you will get warning to update frames or constrain but don't update it)
Step 4 :- Count Accurate Multiplier.
imageviewHeight / superview's height = Accurate Multipliar
Here ,
imageviewheight = 200 , superviewheight=600 so ,
200(imageviewheight)/600(superviewheight)=0.333
(set multipler to imageview where proposanal height = superview)
Step 5 :- check output to different simulator.
I hope this detail explanation is enought to understand your question.
you can give equal height or width with superview with different multiplier. for example multiplier is 0.5 five then your height or width will be half of superview's height.
hope this will help :)
Anchor the edges of the image view as you want. so that the size of image view will change based on device. But set the mode of image view to "Aspect Fit" which will make sure the image will resize but maintain the aspect ratio. You can change mode in Attribute inspector.
Please refer Difference between UIViewContentModeScaleAspectFit and UIViewContentModeScaleToFill?

iOS: Constraining View with fixed aspect ratio to trailing edge OR bottom edge

I have an ImageView that is constraint to be a square and on its top edge:
http://i.imgur.com/dACmwPN.png
The problem is that I want the view to fill the entire bottom space without exceeding the superview. In other words it should conform to either the width or the remaining height depending on which one is smaller.
The guide I am following suggests to add all of these to a stack view, but that is an iOS 9 feature and I want to develop for iOS8. What would be a good solution for that SDK?
I would add width = height constraint to keep the aspect ratio, then bottom and trailing constraints with lower priority (900) and >= margin value (ex 8) . This way image will be a square and it will not go over bottom or right. Also aspect fit should be set.

Xcode 6.3.2 - iOS - Ambiguous UIImageView

I want a UIImageView to expand based on the actual image's height and width. Specifically, I take the image's height and width, and then divide both by ([larger value] / 300) so that the aspect ratio is maintained and the image is optimized to fit in the space I have designed for it (a width that is never smaller than 320 and a height that is always exactly 320). In the code I give the UIImageView the new height and width that I acquired from this procedure. All of this works exactly as I intend it to when viewed in the iPhone Simulator. However, Xcode is giving me warnings about my constraints for the UIImageView, saying that the "position and size" are ambiguous. What is the correct way to make these warnings go away?
Here is the ViewController:
http://i.imgur.com/cVWmVET.png
And here are the constraints I have for the UIImageView in the middle:
http://i.imgur.com/4orUyeh.png
For maintaining aspect ratio and showing image, you don't have to do manual calculations. Just set the following property of UIImageView and assign the image to it
yourimageView.contentMode = UIViewContentModeScaleAspectFit;
For keeping UIImageView in the center of screen, use following autolayout constraints
Constant Height
Constant Width
Horizontal Center in Container
Vertical Center in Container
If you are intending your imageview to expand, then you got to give a bigger values to the Imageview's Content Hugging property,
By Default its 250, give its horizontal and vertical value as 751 as 750 is the default value for Content Compression, so u have to give it a bigger value than that. Tell me if this works.

Resources