7

Is it possible to add custom UIView with autolayout to iCarousel?

If I try to set constraints when custom view is created in iCarousel delegate method

-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view

app crashed because constraints can be added when view is added to superview but at this point it isn't.

I have new xib for custom view. Problem is that my custom view is too large to fit in iCarousel on iPhone, I don't have problem with arranging views in my custom view but I have problem to fit my custom view in iCarousel because I don't know where to set constraints as I sad constraints can't be set in "viewForItemAtIndex" because view doesn't have superview at that point

NFilip
  • 427
  • 4
  • 15

2 Answers2

3

You should try adding constraints to the iCarousel view not for viewForItemAtIndex. If the components inside your view needs constraints you may need to create the view in a new xib file with constraints and invoke that view in viewForItemAtIndex method. I came across the same problem and solved it this way.

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
  CTProductDetailsInMapView *productView;

  if (view == Nil)
   {
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CTProductDetailsInMapView" owner:self options:nil];
     productView = (CTProductDetailsInMapView *) [nib objectAtIndex:0];
   }
  else
    productView = (CTProductDetailsInMapView *)view;
}
return productView

}

CTProductDetailsInMapView is a subclass of UIView implemented in an interface file. Here all the components are given constraints and works properly.

  • I have new xib for custom view. Problem is that my "CTProductDetailsInMapView" is too large to fit in iCarousel on iPhone, I don't have problem with arranging views in my "CTProductDetailsInMapView" but I have problem to fit "CTProductDetailsInMapView" in iCarousel because I don't know where to set constraints as I sad constraints can't be set in "viewForItemAtIndex" because view doesn't have superview at that point – NFilip Dec 08 '14 at 11:51
  • try [view setClipsToBounds:YES] in carousel "viewForItemAtIndex" – Mohammed Shinoys Dec 08 '14 at 11:55
  • and also productView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; – Mohammed Shinoys Dec 08 '14 at 11:59
  • You dont get to see the view or its not in proper size ? – Mohammed Shinoys Dec 08 '14 at 12:08
  • I don't see view when I setClipToBounds to YES. With autoresizingMask I don't see any changes. – NFilip Dec 08 '14 at 12:12
  • try [productView setClipsToBounds:YES] – Mohammed Shinoys Dec 08 '14 at 12:18
  • I added the following line to make it resize to the iCraousel view `productView.frame = carousel.frame` but then the layout constraints are being recalculated only for the first view and not for all of the others and I can't understand why. Any clue? – Valerio Santinelli Mar 24 '15 at 14:11
  • 2
    To resize it you need to add width and height constraints for "CTProductDetailsInMapView" and create property outlets to the corresponding class. Then you may resize it as . [productView.widthConstraint setConstant:carousel.frame.size.width]; [productView.widthConstraint setConstant:carousel.frame.size.width]; – Mohammed Shinoys Mar 26 '15 at 08:28
  • Usually the frame of carousel is much bigger than the view's frame. Since the carousel frame refers to the area in which one or more child views appear depending on your requirements. – Mohammed Shinoys Mar 26 '15 at 08:34
  • @MohammedShinoys: Not sure what you mean. Could you please elaborate that related to (Container: - the view controller that holds the view and has the iCarousel in it and Child: - the UIView or a custom one).? – el.severo Mar 27 '15 at 16:18
  • 1
    @MohammedShinoys - thank you so much! I have been searching forever for that answer. – Josh Gafni Aug 06 '15 at 07:15
1

My solution was to set up frame.size.width / height like:

// current view is view from xib
// don't set frame, just sizes
currentView.frame.size.width = 100 // or other value
currentView.frame.size.height = 100 // or other value

return currentView

Hope it helps to someone

Vlad Pulichev
  • 3,162
  • 2
  • 20
  • 34