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.
Related
I am a beginner, I am trying to do an autolayout. I am trying to make the label 'HRIS' and 'Please login to continue' to be proportional to the screensize (its superview), I can do it for the login button and the image leaf as the picture above, we can see that the login button and leaf image are proportional.
for the image leaf, I do equal width with 0,1 multiplier to the superview, and then I also add 'aspect ratio' to make it proportional.
but when I apply the same way to the label, it doesn't work, as we can see the label size still the same for iphone 4s and iphone 8, maybe it is because the instrinsict content size (the font size itself) that makes it like that. I tried to apply this way, but it seems the size is still the same
I want to make it little bit smaller for iphone 4s, I also can't modify using size classes since it is in the same class
so what I have to do to make label proportional to the superview (screen size / background) ?
You can do it by setting both proportional width of the label and allow it to scale down the point size.
Set the label width to proportional width like this. It is just a regular same width constraint that you edit.
Then allow your label to scale down the point size like this. This is a property of your label.
Set different font size using variations...
Simply set up a constraint for your label's width to be equal the view's width with your desired multiplier. In my example I used a multiplier of 0.5:
i have this problem in 2 projects.Normally, profile image is displayed in circle like
and this is ok for iphone5 size.problem raises on bigger devices.like
as you can see size of profile imageview, it looks small compare to cover image.if i let it increase based on outer image then xocde ignores the aspect ratio constrains which results in non cricular profile imageview
here's current constrains image
any suggestion how should i handle it?
Update
here's result if i set constrain on profile imageview to increase with ratio to background image.
asks for y of image view,as i want it to be center so i sets fixed hight = 68 in hopes that xcode will break this constrain to keep aspect ratio, result
so if i remove hight = 68 and add constrain like width.
result
Select your image view and it's super view together and give equal width constraint,
then select that equal width constraint from size inspector, and reduce its multiplier until you get your desired width!
Now select your image view and give aspect ratio constraint!
And finally give position constraint for x and y position!
In your case center x and center y with it's super view I think!
And your imageview will increase or decrease with device width and will remains square always!
And You should set dynamic corner radius(half of width or height of your imageview) in layoutSubviews method of tableviewcell's class as you need rounded imageview and your size is dynamic!
Instead of setting the height and width as constants, set equal width constraints between the background image and the profile image:
And then select the profile image and set the multiplier value to the desired value:
Now you can also set an aspect ratio for the profile image.
This is simple:
Provide Aspect Ratio constraint and make the width of the profile pic = width of the screen
Select the equal width constraint and give the ratio in multiplier
Just simple question.
How to make the size of the UILabel following device size?
for example, I have 1 label that have size 20 running on iPhone 5. Then if running on iPhone 6 the label size changed to 40. What should I do?
If you are using autolayout and you want to increase height and width of label with respect to screen size then you should simply give four constraint to label like : top,bottom,leading and trailing
If you want to increase height and with with in specific desired value, for example if you want label width exact half of screen width, In this type of case you should give constraints like : leading,top,fix height, fix width. Then, select fixed width constraint and from size inspector change it's multiplier to 0.5. so your label width always remains half of screen width. you can do same for height. and you can set different multiplier value to get desired output.
Hope this will help :)
Normally I will use ratio to define the constraint. For example, if your label height design is 40 px for iPhone 6, I will define the label constraint by setting equal height of the label and the main view. After that change the multipler of the constraint to 40:1334. The label will have exact same ratio of the screen.
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?
I'm just to create an image that spans the entire screens width. The image is a square (512x512) and the idea is to scale to the width of the device while maintaining aspect ratio. I've tried doing this with contraints, setting the margins to left: 0, top: 0, right: 0 and keeping the image at aspect-fill. The issue however, is that the image is becoming larger than the screens width, and thus the image is not only being cut off on the left and right, but is much taller than it should be.
How can I properly size an image to have the same width/height has the width of the device?
The problem is you didn't set constraints on the image view.
Set the following constraints on image view:
1) Set width of image view equal to width of self.view
2) Set aspect ratio 1:1 // This makes sure your image's height is equal to width
You would need additional constraints to specify the x and y position of the image view.
Add constraints as shown in screenShot
Here for Positioning image view, I have given horizontal and vertical center constraints, that you can change according to your requirements(i.e wherever you want to place your imageview.)
Main idea here is to give these two constraints image view -
1.ImageView.width = superview.width
2.ImageView's aspect ratio(width/height) = 1.