Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom UIView with autolayout to iCarousel

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

like image 992
NFilip Avatar asked Dec 08 '14 11:12

NFilip


1 Answers

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.

like image 116
Mohammed Shinoys Avatar answered Sep 28 '22 22:09

Mohammed Shinoys