【问题标题】:Adding custom UIView with autolayout to iCarousel将带有自动布局的自定义 UIView 添加到 iCarousel
【发布时间】:2014-12-08 11:21:07
【问题描述】:

是否可以将带有自动布局的自定义 UIView 添加到 iCarousel?

如果我在 iCarousel 委托方法中创建自定义视图时尝试设置约束

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

应用程序崩溃,因为当视图添加到超级视图时可以添加约束,但此时它不是。

我有用于自定义视图的新 xib。问题是我的自定义视图太大而无法放入 iPhone 上的 iCarousel,我在自定义视图中排列视图没有问题,但我无法将自定义视图放入 iCarousel,因为我不知道在哪里设置约束因为我很难在“viewForItemAtIndex”中设置约束,因为此时视图没有超级视图

【问题讨论】:

  • 当您添加到超级视图时,setTranslatesAutoresizingMaskIntoConstraintsfalse
  • 我已经这样做了

标签: ios objective-c iphone icarousel


【解决方案1】:

您应该尝试向 iCarousel 视图添加约束,而不是为 viewForItemAtIndex。如果视图中的组件需要约束,您可能需要在带有约束的新 xib 文件中创建视图,并在 viewForItemAtIndex 方法中调用该视图。我遇到了同样的问题并以这种方式解决了。

- (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 是在接口文件中实现的 UIView 的子类。这里所有的组件都被赋予了约束并且可以正常工作。

【讨论】:

  • 我有用于自定义视图的新 xib。问题是我的“CTProductDetailsInMapView”太大而无法放入 iPhone 上的 iCarousel,我在“CTProductDetailsInMapView”中安排视图没有问题,但我无法在 iCarousel 中安装“CTProductDetailsInMapView”,因为我不知道在哪里设置约束,因为我很难在“viewForItemAtIndex”中设置约束,因为此时视图没有超级视图
  • 在轮播“viewForItemAtIndex”中尝试[查看 setClipsToBounds:YES]
  • 还有 productView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  • 要调整它的大小,您需要为“CTProductDetailsInMapView”添加宽度和高度约束,并为相应的类创建属性插座。然后您可以将其调整为 . [productView.widthConstraint setConstant:carousel.frame.size.width]; [productView.widthConstraint setConstant:carousel.frame.size.width];
  • @MohammedShinoys - 非常感谢!我一直在寻找那个答案。
【解决方案2】:

我的解决方案是设置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

希望对某人有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2016-02-26
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多