1

How can i set the background image keep fixed in same place while i scroll the content.Now i scroll content means my background images also keep movingbackground image also scroll while scroll content in UISCROLLVIEW..My sample code Posted Below.

My Code here

    - (void)viewDidLoad
{
    NSLog(@"Welcome to Home Page");
    [super viewDidLoad];

   // self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];

    UIImage * img = [UIImage imageNamed:@"bg-image.png"];
    [scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
// Do any additional setup after loading the view, typically from a nib.
}



- (void)loadView {


    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
    scrollView.contentSize=CGSizeMake(1400, 100);

    UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti01.png"]];
    tempImageView2.frame=CGRectMake(10, 60, 200, 200);
    [scrollView addSubview:tempImageView2];


    UIImageView *tempImageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti02.png"]];
    tempImageView3.frame=CGRectMake(240, 60, 200, 200);
    [scrollView addSubview:tempImageView3];

    UIImageView *tempImageView4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti03.png"]];
    tempImageView4.frame=CGRectMake(470, 60, 200, 200);
    [scrollView addSubview:tempImageView4];

    UIImageView *tempImageView5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti04.png"]];
    tempImageView5.frame=CGRectMake(700, 60, 200, 200);
    [scrollView addSubview:tempImageView5];


    UIImageView *tempImageView6 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti05.png"]];
    tempImageView6.frame=CGRectMake(930, 60, 200, 200);
    [scrollView addSubview:tempImageView6];

    UIImageView *tempImageView7 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti06.png"]];
    tempImageView7.frame=CGRectMake(1160, 60, 200, 200);
    [scrollView addSubview:tempImageView7];



    self.view=scrollView;
    [scrollView addSubview:tempImageView2];
    [scrollView addSubview:tempImageView3];
    [scrollView addSubview:tempImageView4];
    [scrollView addSubview:tempImageView5];
    [scrollView addSubview:tempImageView6];
    [scrollView addSubview:tempImageView7];

    scrollView.userInteractionEnabled = YES;
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(22, 100, 1800, 500);
   // [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonTest:) forControlEvents:UIControlEventTouchUpInside];    
    [scrollView addSubview:btn];


}


- (IBAction)buttonTest:(id)sender {
    MSDescriptionpage *aSecondPageController = [[MSDescriptionpage  alloc] initWithNibName:@"MSDescriptionpage" bundle:nil];        
    [self.navigationController pushViewController:aSecondPageController animated:YES];        
    [aSecondPageController release];

}

1 Answer 1

4

It's scrolling because it's the background of the scroll view. So when the scroll moves, the background moves. You could make the scroll view's background transparent (probably by setting the background color to [UIColor clearColor] and setting opaque to NO), and then putting a view behind the scroll view that has the same frame. This won't move with the scroll view. Remember that scroll views move their content, not themselves. The background is a part of that content.

EDIT:

Change this:

- (void)viewDidLoad
{
    NSLog(@"Welcome to Home Page");
    [super viewDidLoad];

    self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];

    UIImage * img = [UIImage imageNamed:@"bg-image.png"];
    [scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
 // Do any additional setup after loading the view, typically from a nib.
}

To this:

- (void)viewDidLoad
{
    NSLog(@"Welcome to Home Page");
    [super viewDidLoad];

    self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];

    UIImage * img = [UIImage imageNamed:@"bg-image.png"];
    [scrollView setBackgroundColor:[UIColor clearColor]];
// Do any additional setup after loading the view, typically from a nib.
}

And then something like:

UIView *customScrollBackground = [[UIView alloc] initWithFrame:scrollView.frame];
customScrollBackground.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];
[customScrollBackground addSubview:scrollView];
Sign up to request clarification or add additional context in comments.

7 Comments

where i want insert [UIColor clearColor] in loadview or view didload
self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]]; i using this comment also for background image...But for this also background moves while scroll the content..
UIView *customScrollBackground = [[UIView alloc] initWithFrame:scrollView.frame]; customScrollBackground.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]]; i want to use this comment inside loadview or viewdidload.... [customScrollBackground addSubview:scrollView.view];
i using this loadview and view didload [customScrollBackground addSubview:scrollView.view]; i got a error in view like "view not found an object of type uiscrollview "
i using above code in view did load...But totally background image is not displayed...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.