问题描述:

Why does the background image won't appear on my scroll view?

-(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];

}

I want to make a scroll view which scrolls horizontally to a long rectangular image at the background. Please help

网友答案:
menuScrollView.contentSize = backgroundImage.size;

set the contentSize in viewDidAppear

And if you want to pan image using scroll view, you must add the imageView to scrollView, not to self.view.

网友答案:

here is a code I use to put a background image below a tableView, should work with a scrollview (tableview inherit from 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;

By adapting it:

-(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];

}

网友答案:

Try adding your scrollView to your self.view:

[self.view addSubview:menuScrollView]

then:

[menuScrollView addSubview:imageView]

Let me know what happens. ;)

相关阅读:
Top