UIScrollView scroll viewcontrollers broken autolayout - ios

I have 2 view controllers which works properly good when I presentViewController them. Now, I've created a ViewController with a UIScrollView to scroll the ViewContoller as 'Scroll UIviewContoller Apple's Example' When I enter in HomesViewController (the controller which the UIScrollView that control the others UIViewController) the layout is total broken, size and positions. When I go out to another page and back again to HomesViewController, the layout is working, respecting the autolayout constraints. I have already tried to implement -(void)didAutoLayout and reload InputViews, but it doesn't work. I will not paste my code, because the ScrolLView works, when I go out from the page and back again...then I think its a problem with refreshing layout...

Look into UIPageViewController it is an excellent way to create a paged "scrollView" of view controllers. This is often used for things like app tutorials.
Apple has already done the work for you :)

Related

UITableView leaving gap below transparent UINavigationBar (within single UIView on UIViewController)

I am working on a Swift iOS8/9 project and using Xcode 7, with storyboard / autolayout, etc. (For completeness: I started at the beginning on the year on older versions of Xcode, etc.)
I have a a UINavigationController hierarchy and some View Controllers contain a single UIView which contain a single child, a UITableView. (I am choosing not to use UITableViewController, so suffice to say ... it is not an option.)
The problem: The tableview starts 64 pixels (status plus nav height) too low:
Now: I've had things working for some of my UIViewControllers containing a UICollectionView within a UIView but I achieved it (after lots of poking / prodding, and a while back) by a top alignment constraint from the top of the UICollectionView to the top of the Top Layout Guide. (Looking through my storyboard I some alignments and some vertical distances, but all to top of Top Layout Guide.)
I am looking for help understanding if I can do what I want, if the approach of constraining the top of the tableview to the top of the layout guide is correct, or if there is a better way.
Unfortunately, I cannot seem to repeat this (perhaps 'cos I've forgotten some key press combination jujitsu, or new Xcode, or collection != table, or something in my view hierarchy. (I've checked all segues are adaptive push, I've even deleted/re-built this VC since I had one model segue, just in case it was confused that this VC isn't in a navigation view controller hierarchy.)
If I control/drag to connect or select the two to align:
this is what I am offered - note no "align to top":
Note: Here are the table settings that (to my understanding) should be adjusting edge inset and scroll offsets correctly, and do appear to ('cos it is the only child of the top view, per documentation.)
Here are the frames after view did layout subviews:
The approach that I ended up using came to me when I spoke to #ProgrammingBonn and mentioned the frames. I saw that the primary UIView was position as I wanted, and so it (finally) dawned on me to align the top of UITableView to the top of the UIView.
(I suspect that much as I was building my own ViewController and not relying upon UITableViewController I had an over inflated view of this particular view within the hierarchy. It was a subview not a primary view, despite occupying all the screen real-estate.)
So, to be clear:
I selected the UITableView, then command/selected the UIView (the main one, and it's parent) and then used the "align" tool to top align those two.
Try unchecking the "Adjust Scroll View Insets" option.
I am not sure whether this will fix your problem but have you tried playing around with viewController.edgesForExtendedLayout?
See more info here:
Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

Weird behavior with Auto Layout and UIScrollView paging

I'm trying to have a horizontally scrolling UIScrollView with horizontal paging (kinda like a UIPageController but managed by a single UIViewController)
Because I'm using Auto Layout I have set up everything by using constraints for the views inside, and the scrollview works just fine. However what is odd is that the paging works in a weird way.
Specifically the scrolling becomes really slow and it takes over 5 seconds for the scroll view to snap in place.
I am absolutely puzzled by this because I have done this before with a smaller view (about three "pages" as opposed to 6) and it worked flawlessly.
I have created a sample project to illustrate this; just build it and try scrolling left and right - it works best if you just flick once.
http://mbx.cm/t/MmLIi
The only thing that seems to prevent this is having the viewcontroller manage the scrollview directly (as opposed to have it inside a UIView), but I'm sure the problem lies elsewhere so I figured I'd try my luck to see if any Auto Layout gurus can help me out on this one.
All I did was change your view controller's "Simulated Size" to "Fixed" instead of "Freeform" in the storyboard and it worked fine after that. Not really sure why that would change anything, but it did!

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!

Add a ScrollView Manually or Programmatically

I have an existing view with several elements, I was thinking of how to include a scrollview to contain all the existing elements without having to go through redesigning one by one the elements,
My objective is, that when my orientation changes to landscape, the elements behind can be scrollable specially when a keyboard is currently displayed.
Can this be done manually or programmatically, and how? Or is there any best approach for this type of scenario's?
*Edit (for clarity)
My apologies for the lack of clarity with the initial question;
I have a view with 3 Textfield, A button and 2 switch, Initially my app only supported the "Portrait" orientation, however, recently I have decided to add "Landscape" orientation support, I already did this. Now my problem is when I'm on landscape the view elements gets covered by the keyboard (specifically). So I was thinking if I could include a scrollview and put the views existing elements(objects) without having to re-design the nib, what I have done already;
I drag a scrollview into the view then making all the existing elements as children of the scrollview so the hierarchy looks like;
--view
--scrollview
--textfield
--button
The end product I get with this approach is that all the elements stacks on top of the other, so I've lost my original design.
So this is what Im asking, is there anyway, to add a scrollview without having to go through the design phase again.
TIA
If your view is designed in a nib, from IB you can select all the elements of the view and then go to Editor->Embed in->Scroll View from the menu.
Following are the steps-
1.Add the scrollview to the main view as subview.
2.create a new view which contains all your elements for the scrollview.
3.add the new view to the scrollview as subview.
4.set the content size of the scrollview as the size of the new view.
Change the frame of the newView accordingly on orientation and repeat step 4.
Thats all you need to do..

Why is the UIPageViewController resizing its UIViewControllers views after the page turn?

Within the pageViewController:viewControllerAfterViewController: method, just before the return statement, the view which is about to be returned as the next page has the correct view frame size.
However immediately after the pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: method is called, I check the frame size of the newly introduced view controller ([pageViewController2.viewControllers objectAtIndex:0];) and I find it resized.
Note that, I have set [self.pageViewController.view setAutoresizesSubviews:NO] and the autoresizing mask to None for the newly created ViewController.
Any ideas in which step the new ViewController is being resized?
I think the problem is inherently related to the nature of UIPageViewController. It is built from UIScrollView. I don't exactly know why there is strange resizing behavior, but it seems to be particularly pronounced when the view controllers that make up your pages use auto layout. Seemingly, locking the constraints in your page view controllers to the superview makes the elements resize after the transition because the superview is itself getting resized after said transition.
This sucks because Apple is basically pushing all of us to adopt auto layout. Auto layout is awesome, and I recommend everyone use it from now on, but it really really sucks when you use it with a UIPageViewController. They really ought to either scrap that class or build something easier for developers, something that can be dragged into a storyboard outright.
A few things to consider.
1.) Don't lock anything to the "Top Layout Guide" or the "Bottom Layout Guide". Also make sure you have "Constrain To Margins" disabled on any view intended to hug the sides of the screen.
2.) If you are using a label in your individual page / content view controllers, make sure you bind/constrain it to something other than the superview. I wanted to place a label over a UIImageView, so I aligned the label to the leading and top edges of the image view (using AutoLayout constraints only), creating an offset to give the label some margins.
3.) The following would otherwise be a good tutorial. However, it is a bit outdated. I downloaded the project and basically modified it to get a UIPageViewController implementation that works. The only problem with this project is that it doesn't use AutoLayout. I'm currently writing a blog post that more clearly discusses how to use UIPageViewController and Autolayout together.
http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/

Resources