UIScrollView的背景图像不会出现在屏幕上

时间:2014-05-05 07:58:46

标签: ios image uiscrollview

为什么背景图片不会出现在我的滚动视图中?

-(void) loadView {
    CGRect screenRect = [[UIScreen mainScreen]bounds];
    UIView *menuView = [[UIView alloc]initWithFrame:screenRect];
    self.view = menuView;

    UIImage *backgroundImage = [UIImage imageNamed:@"Spacemenu.jpg"];

    UIScrollView *menuScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    menuScrollView.backgroundColor = [UIColor clearColor];
    menuScrollView.contentSize = backgroundImage.size;
    UIImageView *imageView = [[UIImageView alloc]initWithImage:backgroundImage];
    [self.view addSubview:menuScrollView];
    [self.view insertSubview:imageView belowSubview:menuScrollView];
}

我想制作一个滚动视图,它在背景中水平滚动到一个长矩形图像。请帮忙

3 个答案:

答案 0 :(得分:0)

menuScrollView.contentSize = backgroundImage.size;

contentSize

中设置viewDidAppear

如果您想使用滚动视图平移图片,则必须将imageView添加到scrollView,而不是self.view.

答案 1 :(得分:0)

这是我用来将一个背景图片放在tableView下面的代码,应该使用scrollview(tableview继承自scrollview)

UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
backgroundImage.frame = self.view.frame;
[self.view insertSubview:backgroundImage atIndex:0];
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = nil;

通过改编:

-(void)viewDidLoad {
    [super viewDidLoad];
    ...

    //create your scrollView
    menuScrollView.backgroundView = nil;
    menuScrollView.backgroundColor = nil;

    UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
    backgroundImage.frame = self.view.frame;
    [self.view insertSubview:backgroundImage atIndex:0];

}

答案 2 :(得分:0)

尝试将scrollView添加到self.view

[self.view addSubview:menuScrollView]

然后:

[menuScrollView addSubview:imageView]

让我知道会发生什么。 ;)