View width issue in iPhone 4? - ios

I have a view on my screen.I have taken iPhone 5 as base screen.I have given following constraints to the view.
1.Aspect ration to self.
2.Aspect ratio to parent view.
3.Horizontal center in container.
4.Vertical Spacing.
Please check this
Here on iPhone 5s the view is not stretched to full width but on iPhone 4 view is not fully stretched.Please tell the issue here.

Ok, I recreated your constraints and was able to replicate your problem. It is quite simple, and is caused by the aspect ratio constraint to superview.
As you can see, the constraint is for text field width to superview height with ratio 320:568. Because we already know the height of superview when drawing the textfield, we need to solve this equation, to get text fields width :
320 textfield.width
----- = -------------------
568 = superview.height
So on iPhone 5, the height of superview (in case of full screen view, of course) is 568 - so we know that textfields width should be 320. On iPhone 4 on the other hand the height is 480. Applying to the equation, and solving for textfield.width we get 270. So it actually shouldn't be stretched.
There is no "issue" here, and everything is working as it should.
If you want the text field to have the same width as it's superview, you should add an "Equal Widths" constraint.
Let me know if you need further explanations.

Related

Xcode how to make flexible constraint in Autolayout

hello everyone I've get stuck in Auto-layout issues
I have login storyboard that contains three Items Image-view , Stack-view , Button like the first image below
I've tried to add constraints using two ways:
The first way I've added "pinned" constraint to Image-view as shown in Photo1 below but when I've changed to landscape the Image was disappeared as the second photo below.
please notice I haven't got any warnings
The second Way I've tried by wrap the Image-view using view then i added
"Pinned" constraint to view
finally i added fixed width & height to Image-view and Horizontal & vertical in container but I've got this result as third and fourth photo
what should i do to make my Image-view Adaptive for all Screens ?
thanks
Photo1
Photo2
Photo3
Photo4
As when changing to landscape screen height changing a lot. the problem with your design with Autolayout is it's not adaptive with landscape and portrait mode because you are give fixed hight and width to your layout.
to make your image-view Adaptive for all screens give your image-view height to superview height in percentage so when in landscape mode height will change your image-view will still gets the height from superview height in exact same percentage.
have a look at this demo hope this will help.
Autolayout
First drop UIImageView into ViewController.
Give it a top constraint and Align it horizontally to it's SuperView.
As now it's only required height and width.
Then Ctrl drag from ImageView to itself and select aspect ratio. give
a aspect ration 1:1 (it's just telling storyboard if any of height or
width is available i want the other to be the same like if width is
given then height should be the same as width, vice versa for height) as you may want
perfect square shape for your profile ImageView.
Now Ctrl drag from ImageView to SuperView and select Equal height.
As now your ImageView got the height to SuperView Height.
give your ImageView proportional height (percentage, Aspect
Ration or Multipiler) to SuperView how much you wanted to give like
so.
At the end your UIImageView will get the height proportional to SuperView height, vice versa for width.

Is it possible to set width and height for different iPhone screens?

I'm working with my project on the storyboard and I have a problem with screen sizes. It looks simple to solve but not for me. I have an imageView, and its width and height are looking good for iPhone 7/7+ screens. But It is not looking good for 5 or SE Screens.
Can I set different width and height for 5 screens? for example, while 7 and 7 pluses run with 200 width and height, iPhone 5 screens will run with 100 width and height. Or is it possible to squeeze the imageView? When I set the constraints imageView it also needs width and height for looking well, but it prevents to squeeze the imageView as well.
If you want the imageView to squeeze, you shouldn't give it a static width or height in the constraits.
One way to go is to set the leading, trailing, top and bottom constraints for it ==> no need for a width or height constraints.
Another way is to give it a proportional width and height, and this can be done by giving the image view an equal width and equal height constraints to its parent view for example, and then set the multipliers of these constraints to some number, 0.5 for example, this way the image view will always take half of the parent view's width and half of its height.

Change UImageView size using Autolayout

I have a imageview at the start and below there are series of buttons placed vertically one below the other. So, while designing the screen, I placed all the views in the view controller and added to vertical stack. Centred stack view horizontally and vertically. For all the buttons, I have set equal width. And in stack view, I have given equal spacing. In all iPhones, it is working correctly with ui elements arranged correctly with equal spaces except iPhone4. In iPhone 4 it is all touching each other. Please find the attachment. What constraint am I missing here?
The problem is the content cannot be fitted in given height.
Let me explain.
Assume small device has height of 480. UIImageView height(intrinsic ) is say 200. You have 4 UIButton of height say 80. UILabel of height say 100. So that sums up to 620.
Views of height 620 cannot fit(all visible without clipping) on a view of height 480.
You need.
Scrollview(To show more content than the available width and/or height)
Reduce the size of you content to fit in 480. To do this you can pin the height of any/some/all to make this height sum less than or equal to 480.
It's because the screen size for iPhone 4 is 480. Your UI seems to be higher than 480. So only the constraints adjusted according to screen height.
Just add height constraint to all button and remove height constraint for image view if added.
Add Height and margin constraints for Buttons and only margin constraints to image view. It will resize according to screen size.
Hope it helps.

Button size varies in iphone 4 and iphone 6

I have one project where there is one button.
Which seems so big in iphone 4 as per the size in iphone 4 resolution
and same button looks small in iphone6 as per the size of the iphone 6 resolution.
I had used autolayout.
But is there any solution to manage it by autolayout or constraint.
Instead of managing size i.e hieght and width programmatically ?
Give that button an equal width constraint with its super view.
Select that equal width constraint and give multiplier value as (width of button)/(width of superview)
then give aspect ratio to the button.
Now the width of button varies with the width of device and height will change in proportion with its width.
I use this method and it works good.
If you want to manage size of your button, with the ratio of iPhone/iPad screen,
First you need to set equal width and height constraint to your button in storyboard.
Then in code, you have to add two IBOutlet of NSLayoutConstraint and connect them to respective constraint in storyboard, then update yourButtonWidthConstrant.constant and yourButtonHeightConstrant.constant from code according to the ratio.

TextField size issue on iPad?

I want my app to run on every device fine.I have text field on the screen.I am giving constraints to text field as follows:
Aspect ratio to self
Aspect ratio to parent view
I this case my text field will scale according to screen size.Now issue is when i use this method then all my textfields look too big on iPad.I have seen facebook app on which TextFileds don't look too big.I look decent so please tell me what method should i use for this purpose.I know i can use size classes but i don't want to use size classes in my project.
Solution i want if a textField is on iPhone 4s is 20px then on iPad it should be 24px with out using aspect ratio & size classes concept.
Without Size classes You can achieve this using the following constraints. I have tested it on only one textfield and it is in center of screen. So overall constraints on this textfield is as follow:
Align center X to SuperView
Align center Y to SuperView
Width >= 168
Trailing Space to Superview Equals 270(approx) (priority = 999)
Leading space to Superview Equals 270(approx) (Priority = 999)
Height Equals : 30
What happen here is that On ipad 270 leading and trailing will be satisfied while on iphone having less width so both of these constraints shrink and width >= 68 constraints come in action. The result is show in pictures
IPHONE
IPAD

Resources