View not respecting constraint, why is that? - ios

I have this view that has these constraints
When this view appears and I get its size on viewWillAppear I get the size as 320,568.
For a view that is locked to the left and right borders, nas an aspect ratio of 80:107 and is running on iPhone 5, I should be getting 320 x 428, right?
So why the view is full screen not respecting the constraint?
thanks

The views have not yet been laid out in viewWillAppear:. The frame will be correct in viewDidLayoutSubviews.

Related

myView.bounds.size return's same value for all screen sizes

I have a subView called contentView which almost flush with the superView.
I designed the subView in Storyboard with iPhoneX and added the necessary constraints to stretch for all screen sizes.
On the different screen sizes the view appears to be stretched as defined by the the constraints but the bound.size of the view remains same on all screen size
contentView.bounds.width
the above on all screen sizes prints the following value
375.0
I am adding more subViews to my contentView according to it's width, but on all screen sizes I get the same value.
It seems that 375 is the width in points for iPhoneX on which I originally added the contentView.
Is there any reason for this as Googling for this problem didn't help.
I would really appreciate if any one points me in the right direction.
Here are my constraints
If you expect constraints to be applied in viewDidLoad(), well it's too early. Views are only assumed to be constraint-ready in viewDidLayoutSubviews().
Add
self.view.layoutIfNeeded()
print(“\(myCustomView.bounds)”)
Then you can print the correct bounds.
This forces the view to update all sub views to the the constrained sizes before you check it. In viewDidLoad that is still going on so this makes sure that has happened.
You can do this in viewDidLayoutSubviews() as well but if the view is a subview of a subview it may not be correct even in that function.

Why UIView is wider than Screen

I've created a view controller using interface builder, while size classes is on.
the view has leading and trailing constraints to it's super view which is the view controller's view. something like this.
|-(14)-(view)-(14)-|
When i'm running the app it looks great, the view has those 14pt insets on both sides, though when I print it's width I get 576 which is wider than the screen width. In interface builder the view's frame width is set to 576 (as the canvas size when size classes is on) but at run time the constraints should override this I guess.
How come the view appears right, but it's frame shows a bigger width ??
When did you print the width ?. I guess in ViewDidLoad. Because you use Interface Builder and enable size classes, so by default, the width and height are 600x600. At the time view did load, the size still refer the size from Interface builder until view did appear, it will be automatically scaled according to the screen size. That why your UIView is wider than the screen.
Here is what I printed out from a view controller built using Interface builder and enable size classes. Run on iPhone 6S plus
[Size] at view did load {{0, 0}, {600, 600}}
[Size] at view did appear {{0, 64}, {414, 623}}
You can use the below methods to update your view:
// Allows you to perform layout before the drawing cycle happens. layoutIfNeeded forces layout early
[view setNeedsLayout];
[view layoutIfNeeded];

View width issue in iPhone 4?

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.

How to get the real size of a view with auto layout?

I have a view in interface builder. I set the width as 600, height as 400. Then I added some constraints to make sure it display correctly on different screen-size devices. The constraints are like: 'trailing space to superview:0, leading space to superview: 0....' Here superview is the main view of the ViewController.
When simulating the app, the view displayed well, and I printed out the size of the view. The printed results show that both the frame and the bounds are still (600, 400). I thought the width of the view would equal to my screen width. What is the problem here?
I'm running it on an iPhone6. I layout the view in interface builder. The test code is
println("bounds width:\(myView.bounds.width), height:\(myView.bounds.height)")
println("frame width:\(myView.frame.width), height:\(myView.frame.height)")
You can check the size in viewDidLayoutSubviews,at this time,your constraints have been applied to your view

UIScrollView Interaction Area does not update with frame/contentSize

I have a UIScrollView which starts with a portrait aspect and which I subsequently (after device rotation) move to another frame which is a landscape aspect.
Accordingly the content view (A UIView inside the UIScrollView) is updated and I set the contentSize of the UIScrollview.
The issue is - after rotation - the UIScrollView has a new frame, new content and new contentSize but the area inside which the user can swipe still behaves as if it was the original frame/content.
Image 1 shows the original aspect / Image 2 shows the new aspect with the odd area available to the user for their swipe gestures.
As you might expect I've fiddled with quite a few of the settings in the scroll view such as the bounds etc but nothing to date has 'fixed' this behaviour.
Any insights into why this behaviour is happening and/or how to set the user interaction area to the frame/bounds of the scroll view would be greatly appreciated.
EDIT2:
There are 3 views in this hierarchy
Lanscape
mainView Frame {0,0,1024,748}
resultsView Frame {633,90,361,677}
scrollView Frame {20,60,340,595}
Portrait
mainView Frame {0,0,532,1240} <--- Whoa Horsy - I expected .... {0,0,768,1004}
resultsView Frame {0,659,768,345}
scrollView Frame {0,70,768,280}
At this point I went into every nib in the project and made sure that autoresizes was turned off and I also tagged every view with a different number so that at the time I was checking the frames I could be sure certain definite positive of what I was looking at.
The top level view in the stack WAS autoresizing and now with that turned off I am getting this for the portrait result..
mainView Frame {0,0,768,1004} ..... YES! WIN.....
resultsView Frame {0,659,768,345}
scrollView Frame {0,70,768,280}
EDIT1:
Setting the resizing mask as comments below reduces the visible size of the scrollview to match the interaction area - so making it seem that the scrollview really does think it's frame is that size only (768 made to look like 468 or so). Increasing the width of the frame does indeed increase the size of the scroll view but there are only 768 pixels for the full width so why should a frame thats 768 wide look like 468 and a frame of 686 look like 568?!
It sounds like you've got a superview of your scroll view which isn't resizing correctly. You could verify this by logging [myScrollView superview] and seeing if that view is resizing as you expect.
If the superview is not resizing, then it won't pass touches down to your scrollview, which explains the behaviour you're seeing. (This also explains why it resizes when you set is autoresizing mask.)
have you tried using the following method on the object?
[_yourView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];

Resources