ios UIScrollView have a bad default contentOffset - swift - ios

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.

Related

UITableView contentOffset for headerView misaligned

My headerView in my tableView should go all the way to the top of the device. On iPhone X the same so the notch goes over it.
Then when I scroll my tableView I use scrollViewDidScroll(scrollView: UIScrollView) to change the height of my headerView to add scrollView.contentOffset.y so it goes all the way.
It works as soon as I scroll but not initially.
So it looks like this:
and this on the X
How to I get rid of the white area so the grey one goes under it when it loads?
I tried a couple places but it seems the tableView still has no contentOffsets... I think I am missing something fundamental here how "it should be done"...
Update: ViewHierarchy, blue is tablewView, but then I get this distance there...
Ok I found it. So it has to do with the change with safe areas introduced for the iPhone X.
There are two possibilities, it really depends on the storyboard setup:
If you are using a storyboard and your base is a UIViewController with a tableView placed inside it:
Then fixing the auto layout is the answer:
When safe area is enabled, click on your topmost view and create the top constraint is relative to the superView with constant 0. By default it wants to protect the safe area so it is set relative to the safe area.
If you are using a UITableViewController template (e.g. default when dragging out a navigation controller - tableview combo (my case))
Here there are no constraints that can be set. Hence it didn't work for me (see above).
To fix it click on the root tableView, select the size inspector and then set "content insets" to "never".
Or programmatically in viewDidLoad:
self.tableView.contentInsetAdjustmentBehavior = .never
Are you making your tableview in storyboard?
in viewDidLoad try anchoring your tableview:
tableview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

AutoLayout weird behaviour iOS

When I push to a UIViewControllerB and then pop back to UIViewControllerA entire view of UIViewControllerA is about 20 px higher then it should be and in a moment (I guess after viewDidAppear) it moves to its normal position. I do not manipulate constrains or UINavigationBar settings in any way while this. I do have all elements of UIViewControllerA top space to top layout guide. What might cause this? I've worked with storyboard a few and have never faced something like this.
Try adding view.layoutIfNeeded() in your viewDidLoad or viewDidAppear.
The most recent XCode adds some new behaviors. Try setting the constraints of UIViewControllerA to have the top one be flush with the bottom of the UINavigationBar.
This was somehow caused by removing subview, I've added to cover screen while loading data: after removing this subview, constrains where changed (this only happened on this UIViewController, other UIViewControllers with this subview are fine). I've fixed it by doing this:
self.stopLoading() //removes subview
self.view.layoutIfNeeded()

UIPageViewController resize child view to reserve space for itself

I am developing an iPhone application for ios 7.1. The ViewController hierarchy is like "NavigationController -> UIViewController -> UIPageViewController -> UITableViewControllers". NavigationBar is hidden. Status bar is present. Everything is done programmatically. No storyboard/Xib is used.
In the UIPageViewController, I am creating a custom Title view (similar to the Navigation bar, but inside its own view). Hence the child controller's view (UITableView) should be pushed below this Title View. I have tried adding a constraint ("V:[TitleView]-0-[ChildView]"), after setting translatesAutoresizingMaskIntoConstraints of child view to NO. But this resulted in a blank screen (except for the title view). Changing frame of the child view etc and all did not have any effect.
Apart from setting the above flag and adding the constraint, I have not added/modified any auto layout/auto resize related settings. The application supports only the portrait mode (Home button down). There is not going to be any other dynamic layout change either. Appreciate if you could help in resolving this issue.
Following is the solution I ended up with as of now. Project was on hold for some time, and now being rewritten in Swift. Hence the below code is in swift.
//in the UIPageViewController
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let scrollView = view.subviews[0] as UIView
scrollView.frame = CGRectMake(scrollView.frame.origin.x, 64, scrollView.frame.size.width, scrollView.frame.size.height - 64)
}
Basically I am moving and resizing the Page controller's default scroll view below my title view. I have to admit that I am not very happy with the solution. I feel that there must be some better alternative. (To make it safer, you can iterate the subviews to find the scroll view).
Look at the autoresize constraint from your page view controller and set it to false.
I think it's not the child which are autoresize but the pageviewcontroller frame.

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

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

UIScrollView unwanted scrolling after addSubview or changing frame

I have a UIScrollView filled with subviews, all is well when creating it and initially filling it.
But when I add a new subview that is positionned outside of the visible screen portion, or when I just resize an existing subview that is also outside of the visible screen portion, there is a subsequent 0.3s-long scroll animation (I can see it happening from my delegate) that seems to match the newly added/resized element.
Attempts:
pagingEnabled is always NO.
Setting scrollEnabled to NO during subview manipulations doesn't help.
Doing a setContentOffset:animated:NO after subview manipulations doesn't prevent the animation.
One single giant subview with all my subviews in it doesn't help.
My current workaround is to initially set the frame to fit inside the visible screen portion, or doing resizing work inside another superview, but it feels dirty, and won't handle all situations...
Is there a way to prevent this automatic scrolling animation when programmatically manipulating subviews?
Xcode 4.3, iOS SDK for 5.1.
I too discovered this problem and found this solution http://www.iphonedevsdk.com/forum/iphone-sdk-development/94288-disabling-uiscrollview-autoscroll.html
It involves subclassing the UIScrollView and entering no code in the following method.
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
}
Like the guy says on the link I've found it works and no problems so far. Hope it works for you.
I had this problem because I set the content size of the scroll view prior to adding the subview.
As soon as I change the code so that the content size of the scroll view was set after adding the subview the problem went away.

Resources