【发布时间】:2013-12-20 14:30:47
【问题描述】:
我正在使用iCarousel 来显示我在 IB 中创建的自定义视图的实例。当轮播加载时,轮播中的第一个视图正确显示,如下所示:
但是,其他索引似乎忽略了我的约束,如下所示:
单击此视图后,布局会自行更正。另请注意,第一个索引现在有一个搞砸的布局:
知道如何纠正这个问题吗?我与轮播相关的代码:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
NSLog(@"size=%d",audio.count);
return audio.count;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
NSLog(@"get view for index %d",index);
Audio *aud = [audio objectAtIndex:index];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
AudioView *aView;
float height = screenWidth*.5;
float width = height * (16.0/9.0);
//create new view if no view is available for recycling
if (view == nil)
{
aView = [[[NSBundle mainBundle] loadNibNamed:@"AudioView" owner:self options:nil] objectAtIndex:0];
[aView setFrame:CGRectMake(0, 0, width, height)];
}else{
aView=(AudioView *)view;
}
aView.layer.cornerRadius=8;
NSLog(@"%f",aView.frame.size.height);
NSLog(@"%f",aView.frame.size.width);
aView.backgroundColor=[UIColor alizarinColor];
aView.textLabel.text=aud.text;
aView.titleLabel.text=aud.name;
if (rowCurrentlyPlaying==index&&isPlaying) {
aView.imageView.image = [UIImage imageNamed:@"pause.png"];
}else{
aView.imageView.image = [UIImage imageNamed:@"play.png"];
}
return aView;
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
if (option == iCarouselOptionSpacing)
{
return value * 1.1f;
}
return value;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
NSLog(@"Clicked");
if(index==self.carousel.currentItemIndex){
Audio *aud = [audio objectAtIndex:index];
NSURL *audUrl = [NSURL URLWithString:aud.audioUrl];
if (rowCurrentlyPlaying==index) {
if (isPlaying) {
[anAudioStreamer pause];
isPlaying=NO;
}else{
[anAudioStreamer play];
isPlaying=YES;
}
}else{
rowCurrentlyPlaying=index;
isPlaying=YES;
aPlayerItem = [[AVPlayerItem alloc] initWithURL:audUrl];
anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
[anAudioStreamer play];
}
[carousel reloadData];
}
}
编辑:我也在 iPad 上尝试过,我认为这些屏幕截图可能有助于揭示一些信息(请忽略搞笑的字体大小)。看起来它在未选中时只是没有拉伸到全宽。
正确的布局:
不正确的布局:
【问题讨论】:
-
嗨詹姆斯,你有没有找到答案?我遇到了同样的问题。
标签: ios iphone uiview interface-builder icarousel