Why UIView is wider than Screen - ios

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];

Related

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

View not respecting constraint, why is that?

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.

get height of a content view subview in a scroll view

I'm trying to understand how scroll views works with autolayout.
I have understood that there are 2 ways of getting it to work, the pure version or the mixed version. I'm trying the mixed version, following the recepi on this page: https://developer.apple.com/library/ios/technotes/tn2154/_index.html
I have a scroll view with a contentView as the first subview. According to the apple recepi i need to do this:
[scrollView setContentSize:CGMakeSize(contentWidth,contentHeight)];
I have set my contentView to be 600 pt high, but when i run the app it just logs the screen size and not the actual size of the contentView.
NSLog(#"%#", NSStringFromCGSize(self.scrollViewContent.bounds.size));
This logs {320, 568}
What am i doing wrong?
According to your comments, the view hierarchy as below. Concept behind this, even if you change content size of your scroll view, it won't change size of all subviews.
---ScrollView
-----scrollViewContent
------OtherViews
If you want to change size of content view according to content size, just change size too for content view frame.
CGRect newFrame = self.scrollViewContent.frame;
frame.size = scrollView.contentSize;
self.scrollViewContent.frame = newFrame;

UIScrollView Not Scrolling in iPAD

I have a UiViewController, inside which I have a UIScrollView with the width and height as 768X3000. Inside the UIScrollView I have a view with widht and height as 768X1024.
I am totally confused on how to implement UIScrollView. My scrollView doesnt work. I have lot of contents to be displayed in the UIView and UIScroll doesnt scroll down.
What connections or setting do I have to do make the UIScrollView work.
You need to set the contentSize property of your UIScrollView to 768 x 3000, not its frame or its bounds. So in viewWillAppear you could add code to set your scrollView's contentSize:
self.scrollView.contentSize = CGSizeMake(768.0f, 3000.0f);
1- viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
-(void)viewDidAppear:(BOOL)animated{
myScrollView.contentSize = CGSizeMake(768, 3000);
}
2- another tip you can set contentSize in viewDidLayoutSubviews:
3- set the delegate of your scrollview
myScrollView.delegate=self;
//in case you need the delegate methods
4- If still you cannot scroll the view even after you set contentSize correctly, make sure you uncheck "Use AutoLayout" in Interface Builder -> File Inspector. Reference
If you're using storyboards in Xcode 5 and developing for Auto Layout then there are a few constraints you need for it all to work (in my experience). First, lay your views out like this:
main view
scroll view
content view (put all the stuff you want to scroll in this view)
Then do the following in this order in the storyboard editor:
Set the content view height to 3000. Width should be 768.
Set the scroll view height to 1024. Width should be 768.
Set the main view height to 1024 if in freeform sizing or leave it using inferred sizing. Width should be 768.
Before you do the next steps, just double-check each view's height to be sure nothing changed. Sometimes the storyboard editor makes bizarre changes on its own.
Set a height constraint for the content view only. It should be set at 3000.
Pin the top, bottom, right, and left sides of the content view to the scroll view using 0 for each edge. You will have to manually change the bottom constraint from a negative number to 0. This is very important, so I'll repeat it: manually change the bottom constraint to 0.
Pin the top, bottom, right, and left sides of the scroll view to the main view using 0 for each edge.
Now it should scroll. If you want to ensure that it stays centered when you change to a horizontal orientation, add a horizontal center constraint to the content view as well.
I have many scrolling views in my iPad app and didn't have to use the .contentSize code once if I built my views this way in the storyboard editor.
Good luck! I know what an absolute pain and time-waster this can be.

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