iOS: Why is my subview appearing with a big spacing inside scrollview - ios

I have a single scrollview in my view controller and several subviews (textfields and labels). These subviews appear grouped in the middle of the screen, although I added constraints.
How can I make them appear with the standard spacing to the navigationbar?

It might be a problem with translucent nav bar.
Turn on the nav bar translucent and see if the views are correctly positioned. If they are, you have to adjust the spacing in IB.

Autolayout doesn't work the same way as everywhere else when inside a scrollview--it's a whole new ballgame (as described in apple's documentation).
If you place a UIView within the UIScrollView, and make sure you have that view the same size as the scrollview's content, you can use autolayout in this new view to place all your textfields/labels however you like.
For more info, see apple's docs on scrollviews & autolayout.

setting
self.automaticallyAdjustsScrollViewInsets = NO;
or, alternatively and corresponding in the storyboard, did fix it for iOS7. Don't know if this works for iOS 6

Related

UIModalPresentationPopover - arrow direction Up, Prevent Contentview from starting from tip of the arrow

in iOS 13 it happens that the view of UIViewcontroller that is presented as model starts from the tip of the arrow. Because of it top banners in all of my popover gets cut. Can I prevent this from happening ? I want that my UIView does not start from arrow but once arrow is finished.
To fix this issue, I opened up my view controller's XIB, selected the top-most view, and then checked "Use Safe Area Layout Guides" under the File Inspector. Then I added Auto Layout constraints (of length 0) around that view's sole subview (a UITableView).
(I also needed to update the view controller's background colour. This changed the arrow colour back to what it was in iOS 12 and before.)
Background: https://forums.developer.apple.com/thread/122703
I wonder if your contentView contains UIScrollView. If yes, please add alwaysBounceVertical = YES to your scrollView. It works in my case.
I don't know why they changed this behavior, it can be a pita to deal with it if you are not using xibs. I couldn't find anything in the docs about it.
Use the safe areas to adjust the position of your view. You can both try to extend the size of that red area, or skip the arrow completely. In this particular case you can use safeAreaInsets.top if the arrow is going to be on the top always.

Convert view to scrollview for modal controller

I have some modal controllers that are UIViews and are not scrolling.
Reading on SO, the apple docs and elsewhere, it seems there are three approaches to fixing this:
Multiselect all the elements within the view and then go
editor-embedin-UIScrollview.
change the view to a scrollview in the identity inspector and then
add the following line to viewdidload:
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 2000)];
Copy elements to clipboard, delete the uiview, add a new scrollview
and copy the elements into the scrollview. Warning - this destroys
the positioning of the elements although it does preserve their
outlet properties.
However, I have tried all of these and the scrollview still does not scroll.
Is there any other step I am missing?
Thanks in advance for any suggestions.
Edit:
Getting scrollviews to work is not easy and there is much conflicting advice on the web.
I finally got this to work by setting the content size in a separate method as opposed to viewdidload.
(void)viewDidLayoutSubviews {
self.MainScroll.contentSize = CGSizeMake(320, 1800);
}
Thanks as well to Evo for advice on setting all the vertical dimensions correctly and using freeform in the VC size inspector as it won't work if you don't carefully set these.
If you have EVER enabled autolayout, even if it's disabled now, you need to:
Select the view controller you plan on scrolling
At the bottom right of the storyboard view, click the third button (far right) and then "Reset to Suggested Constraints"
Make sure that all the elements in the scrollview are embedded in the desired positions.
If you are not using autolayout:
Check that the Size of the view controller is set to (320, 2000), or whatever you want.
In the Simulated Metrics of the view controller, you can set the size type to Freeform.
Then in the tab to the right of Simulated Metrics, you can set the width and height to the desired values.
Please comment any concerns

ios UIScrollView have a bad default contentOffset - swift

I have a View inside a ScrollView inside my main View
The problem is that my scrollView have a bad default contentOffset.
His value is (0, -64) in portrait
The Apple doc says
The default value is CGPointZero.
I put this code on my controller to temporary handle it :
dispatch_async(dispatch_get_main_queue(), {
self.scrollView.setContentOffset(CGPointZero, animated:false)
})
Why my contentOffset have not the good default value ?
It's probably an issue with the view insets.
On your XIB/Storyboard for your view controller, make sure to uncheck the Adjust Scroll View Insets
or in your code add self.automaticallyAdjustsScrollViewInsets = false
Swift 4.2+
scrollView.contentInsetAdjustmentBehavior = .never
I found the explanation on this website:
http://www.codelord.net/2013/10/18/adapting-scroll-views-to-ios-7/
Handling navigation bar on top of our scroll view
The iOS 7 view of course comes with the new look where scroll views go under the navigation bar for a nice effect. One can change the scroll view’s contentInset manually to cover for the portion of it that is being overlapped at the top, but doing so manually is tedious and not fun. I was very thrilled to discover handling it should come at no cost if my ViewController has automaticallyAdjustsScrollViewInsets set to YES.
This didn’t work for me out of the box since it turns out for the magic to happen the scroll view has to be the first subview of your ViewController’s UIView. I had to reorder my subviews and then the magic started rolling.

Why the layout is different between storyboard and the simulator?

As below image shows, in the storyboard, I put a UIScrollView (say called A) under navigation bar and another UIScrollView (say called B) inside A. B is at the top of A.
However, in the simulator, there is big margin between B and the up bound of A (I didn't use any autolayout or write any code). Can anyone tell me the reason? Thanks.
add the below line in your - (void)viewDidLoad method
self.automaticallyAdjustsScrollViewInsets = NO;
hope it will fix your issue.
Even though you're not using Auto-layout, you have Auto-layout checked on in your Storyboard. Because you do, UIScrollViews and any Subclasses of it (UITableView, UICollectionView, etc.) all automatically adjust for the UINavigationBar height when in a UINavigationController. This is implicit and there's no way to turn it off. The only solution is to "offset the offset" or to move the UIScrollView away from the UINavigationBar.
The big margin size is navigation bar height, try add a top autolayout for your scrollView.

UIScrollView & AutoLayout - UIScrollView Over scrolling (showing excessive whitespace at bottom)

I have a login page with a bunch of elements on it:
UIScrollView
Content View (UIView)
Email Text Field
Password Text Field
Login Button
Etc.
I need my app to run perfectly in both portrait and landscape orientations so I began setting up the different constraints to get AutoLayout working properly. I finally have all my UI elements in the right positions for both Portrait and Landscape, but the issue is that my scroll view scrolls too much and leaves a lot of empty white space at the bottom of the view when fully scrolled. I would like to fix this and keep things tight but I am not sure why. It's clearly the contentSize of the scrollview that is being set too large for the y value but I do not want to fix this with a workaround programmatically as I'm sure there should be the "correct" fix out there.
I've attached screenshots of the login page normal and fully scrolled (portrait and landscape) to help further understand.
Any help would be greatly appreciated. Thanks!
To adjust the height of scroll view, you need to do these two things:-
In the size inspector of scroll view, change the intrinsic size to "PlaceHolder".
Make sure you have added enough constraints that height of scroll view can be calculated. Since the contents in your scroll view are of static height, you can simply add a height constraint to your scroll view.
I'm not a 100% sure of this, but it could be that the view extends over the nav bar, causing the scrollview to have a much larger height than it should. IF that is the case, then autolayout-ing your view cause elements to be displayed everywhere else, other than the places you want them to be.
You'll need to disable Extends Under Top Bars in the attributes inspector.
I too had problem with scrollview my problem was that in iOS8 the scrollview was leaving some white space from top. I got crazy, finally I saw the solution by Ali Awais in this post and it fixed it
Here is the solution and link by Ali
I faced the same issue in iOS 8, following is solution I found: - Select View Contoller (in storybord) in which you have added the scroll view - In "Property Inspector" in "Layout" section un-check "Adjust Scroll View Insets"
White space scroll view
-anoop

Resources