UIScrollView背景图案图像

时间:2013-08-04 20:34:03

标签: ios uiscrollview uiimage

我正在尝试使用UIButtons作为书籍来实现类似于书架的可滚动图像。

使用第一种方法,我将架子设置为卷轴的UIImage背景。这看起来很完美,除了货架图像不滚动。

这就是看起来像

enter image description here

我尝试了第二种方法,它只是将滚动条的背景颜色设置为货架图像的图案图像版本。

这是我的第二个版本的代码:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    [self.scroller setScrollEnabled:YES];
    [self.scroller setContentSize:CGSizeMake(320, 700)];
    UIImage *shelfImage =  [UIImage imageNamed:@"Shelf.jpeg"];
    self.scroller.backgroundColor = [UIColor colorWithPatternImage:shelfImage];
}

这是(混乱)图像:

enter image description here

主要问题:如何使此图案图像看起来像顶部的第一张图片?

1 个答案:

答案 0 :(得分:1)

您应该使用UITableView代替。在此,您可以轻松设置cell.backgroundView

UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake:(0.0,0.0,320.0, height)];
backgroundView.image = [UIImage imageNamed:@"Shelf.jpeg"];
cell.backgroundView = backgroundView;

如果你真的只想使用UIScrollView,那么只需要一个单元格背景图像,图像宽度应该与scrollView的宽度相同,因为colorWithPatternImage:方法的工作方式与平铺属性相同。

enter image description here

只将此图片带入您的项目,并将其用作backgroundColor的{​​{1}}。

UIScrollView

看起来像这样:

enter image description here

但我建议您再次使用[scrollView setContentSize:CGSizeMake(320.0, 1000.0)]; [scrollView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cellImage.png"]]]; 代替UITableView