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


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.


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

链接地址: http://www.djcxy.com/p/23762.html

上一篇: 源图使用gulp,Browserify,reactify,UglifyJS

下一篇: 将自定义UIView与自动布局添加到iCarousel