Carousel constraints issue
I have a carousel in a view. It is sitting at the bottom center of the view. I have no constraint warnings and the preview shows everything where it should be. When I run the app either in the simulator or on a real device the carousel sticks itself in the top left corner of the view, under the nav bar. I fought with this for an hour or so and in reading another tutorial that mentioned making sure we check the "clip subviews" checkbox in the attributes inspector and darned if that didn't seem to fix the location of the carousel. But later it began exhibiting the same behavior. Now when I start the app and the carousel is stuck in the upper left I stop the execution, do a clean then rebuild it it will behave for awhile.
I have deleted and redone all the constraints to no avail.
What the heck am I doing wrong?
Thanks in advance, Jon Clayton
I had a similar problem and fixed it by wrapping my carousel cards in yet another view.
Say CardView is one of the views that's supposed to be in the carousel. Instead of setting width/height constraints or frame.size directly on it, I add it to a CardHolderView. I set top/leading/trailing/bottom constraints to be equal between the CardView and CardHolderView. Then I instantiate CardHolderView with CardHolderView(frame: CGRect(origin: CGPoint.zero, size: yourSizeHere)) and return it in viewForItemIndex.
I haven't figured out why this is necessary, but I'm suspecting that constraints inside of CardView are messing up whatever iCarousel uses to position each CardView. So putting it inside of CardHolderView lets CardView set up it's constraints referencing CardHolderView, and then iCarousel does whatever the heck it does to position CardHolderView.
I'm also facing the same issue. Can others tell whether is it a common problem or is the way we are adding the custom UIView to the carousel incorrect ?
@andreyrd I've been fighting with this thing for a few days now. Thanks!
@sundeepdev Judging by the reactions I'd say it's pretty common when combining your iCarousel with constraints/frame.
CardHolderView
Can you explain it a bit simpler, unable to understand your scenario.
My case: Storyboard:
- ParentView
- CarouselView (is inside the ParentView) And then Programmatically I am initiating an - Image view and returning the same with "viewForItemAt"
CardHolderView is an additional view I create and instantiate whose sole purpose is to hold the actual view to show.
So rather than instantiating and returning the image view, put it inside of another container view, set up constraints, and return the container view.
CardHolderViewis an additional view I create and instantiate whose sole purpose is to hold the actual view to show.So rather than instantiating and returning the image view, put it inside of another container view, set up constraints, and return the container view.
Thank you!!!