UIScrollView gets created at wrong position for first time - ios

I'm trying to create a scroll view which goes behind of navigation controlled but when the view controller is viewed at the first time scrollview gets created below UINavigation Bar if you scroll it gives a leg and gets adjusted and works fine. I check everything but I didn't found the problem I think it's a Bug
It's Little bit difficult to notice in GIF. I Have Attached a project creating that problem just have a look at it. It's attached below
LINK TO PROJECT

The problem can be solved unchecking Adjust Scroll View Insets as shown below. This stops your scroll view from ignoring you auto layout constraints and stops it from adjusting its bounds from your navigation bar.

Related

Placing views and elements within Scroll View

So I'm trying to get a layout to work using Scroll View.
Correct me if I'm wrong but from what I've been reading around the internet, it looks like to properly use Scroll Views and make it work with AutoLayout you need to have your root view, then put the scroll view inside it with constraints binding it to take the whole size of the root view (left/right/top/bottom constraints to 0) and then adding a view inside the scroll view and once again binding it to take up the whole space.
Afterwards, any ui elements or subviews would go in the innermost view.
My problem is that often I have experienced issues while settings my constraints within the scroll view where for example setting a trailing constraint to 0 wouldn't actually set to the end and so on, the numbers just wouldn't add up. In this case when I'm trying to make the innermost view take the whole space,it ends up messing it up more than anything; see image below.
Any help would be appreciated.
I just created this layout using the follow:
To test it, I added an image view to the content view and put a humongous image in it. It scrolls around quite nicely including bounce etc.
Update: You need to select your ViewController and uptick "Adjust Scroll View Insets"
Hope this helps.

Interface builder position off screen

I have a container view showing a sidebar which is pinned to the main views leading edge. The sidebar is initial visible which is fine for iPads however I would like it to be hide initially for smaller devices. To do that I need to set the side bars trailing edge constraint to be (0 - its own width)
As far as I can see this is not possible in the interface builder. I have tried to do it in the viewDidLoad, checking if the device is an iPhone before doing self.sidebarX.constant = -self.sidebar.frame.width. This fails because viewDidLoad has not set up the views yet so the width is wrong. I also tried to do it in viewDidLayoutSubviews however the user sees the sidebar disappearing which isn't nice. I am sure there must be a common way of dealing with this?
I finally worked it out. viewDidLayoutSubviews was the correct place to be doing this. At first when I tried it, it was showing the sidebar slide away as the view controller loaded. It turns out this is because I was calling my closeSidebar method which animates the side bar moving off screen. Changing this so it just sets the view off screen and adding a check to ensure this only done once on first load (as viewDidLayoutSubviews is called multiple times) does the job of hiding the the sidebar for certain devices without anyone seeing it happen.
You can set this using xcode adaptative layout:
You can set the different position for all different screen types here, changing the constraints, positions, sizes to each different type you need.
You can install the layout of one object in different screen types using the dialog below:
Have a look in this 2 parts tutorial from raywenderlich part 1 part 2

UIScrollView in NavigationController ignores top layout guide

I've seen similar questions about custom transitions(iOS7 Custom ViewController transition and Top Layout Guide and Navigation controller top layout guide not honored with custom transition), but I have problem even with regular push. I'm using latest Xcode available now (Version 5.1.1 (5B1008)).
Here is my storyboard:
Problem occurs in 3rd VC
Here is 3rd VC settings:
My 3rd controller's layout is follows:
UIView
UIScrollView
InnerUIView
Other views
I've tried two different ways to create a layout:
Ignore top layout guide (it has y = 64 because of nav bar)
I pinned scrollview's top to container (ignoring topLayoutGuide), manually set height of inner view and pinned its top to scrollView. It gave me the following result:
Looks fine, but why do I need top layout guide then?
Use topLayout guide
ScrollView's top is pinned to topLayoutGuide.
As you can see, top button moved down and view looks strange.
What is the right way of creating such layouts?
I had the same problem and spent hours pulling my hair out.
My container view inside scrollview had weird top offset despite the fact that it had top constraint set.
The workaround I've found - you have to uncheck Adjust Scroll View Insets in your controller layout options
that way content view (in my case) stays pinned to scrollview's top.
Unfortunately I couldn't find any reasonable explanation of this behaviour.
In my case, Xcode 7 and 8, I had to uncheck the 'Adjust Scroll View Insets' for the Views in my Navigation View Controller. And yes, it wasted too much time before I figured this out.
EDIT: Apple has found out that we have found a solution, so they managed to break this again in XCode 9 and 10, to keep us developers pulling our hair. Haven't found a solution yet.

scroll views working fine in iOS 7 with problems in iOS6

I hate iOS scroll views with auto layout, they are driving me crazy!:-D
I have found a method which works perfectly on iOS 7 to use scroll views:
I put the scroll view directly in the main view, attaching the scroll view to the edges of the super view(top,bottom leading and trailing space equals to 0).
Then I put a UIView into the scroll view attached to the edges of the scroll view, (top,bottom leading and trailing space equals to 0).
Then I set the height constraint of the most internal view, I link it at my ViewController class, and I modify its value programmatically.
Or, if the view it's 'static' I put every component using the storyboard into the internal UIView, starting with the one at the Top attached to the top of the super view, the one under, attached with the constraint 'Vertical Spacing' referred to the one over him..and so on... Until the last, which is also attached to the bottom of the UIView..
Everything works fine in iOS 7, it's perfect, but iOS 6 is messing around with the constraint.
When I first launch the view everything is perfect, but it seems to recalculate the constraints even on the didappear(and in a bad way), in fact, if I leave my view while it's scrolled down, when I come back to it(let's say we are in a navigation controller) , is like it's everything 'moved up' and the components at the top are hidden.
Is there a way to make scroll views work in both iOS 6 and iOS 7, I'n thinking at two storyboards as the only solution, please tell me there is another way... :-)
thanks everyone, I post two images explaining my problem, taken from the top of the screen:
Well uncheck use AutoLayout in file inspector and try.

Embedded scrollview in tabbar item using storyboards won't scroll

For my current project I wanted to use storyboards and autolayout instead of coding everything by hand. It has gone well so far, but my design has a section of the app where there is a tab bar and one of those tab needs to show four views. The design is to swipe between the four and so I thought to use a scroll view. After some trial and error I found that embedding a Container View in the tab allowed me to easily set up a scroll view and put a couple of view inside it, carefully aligning them using positioning to put them side by side so that each page is one child view. I'm not sure how that plays with the autolayout, and in fact I have the problem that the scroll view won't scroll past the first page position. I can drag about 1/3 of the second page into view, but it never brings that page entirely in view.
I've checked the content size and offset and all of the view positions and it all seems correct. And when I use Spark Inspector to change the scroll offset to the position of the second view/page, the app shows the right page/view and I can even scroll back to the first page. I'm a bit perplexed as to what is causing it to not scroll properly. I don't have any code to show as this is all done in storyboards, but I am wondering if anyone has any ideas as to what is wrong?
Alternately, does anyone have an idea for how to use autolayout and storyboards to set up swiping four adjacent views in a tab? I suspect there are ways to do it. I can think of ways to do it in code, but I'm trying to avoid doing it that way.
EDIT: I set the scroll view delegate to the view controller around it and checked the values of contentSize on scrollViewWillBeginDragging and scrollViewDidScroll. It is always set to {0,0} even after I force set it on viewDidLoad. So I tried setting it every time scrollViewWillBeginDragging is called, which seems work, but I don't understand why this happens and it doesn't smell right. Is there some autolayout constraint that might account for this? Does something cause contentSize get set to {0,0} during the layout process?
For lack of any other answer, I'll use my unsatisfying solution as an answer in the hopes that helps someone else in the future:
I set the scroll view delegate to the view controller around it and checked the values of contentSize on scrollViewWillBeginDragging and scrollViewDidScroll. It is always set to {0,0} even after I force set it on viewDidLoad. So I tried setting it every time scrollViewWillBeginDragging is called, which seems to work.
I'd love to know why the contentSize is being reset if anyone finds out!

Resources