2012-07-28 121 views
1

我如何設置背景圖像保持固定在同一個地方,而我滾動content.Now我滾動內容意味着我的背景圖像也保持movingbackground圖像滾動同時在UISCROLLVIEW滾動內容。 。我的示例代碼張貼在下面。背景圖像也滾動同時滾動內容在UISCROLLVIEW

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

} 

回答

4

它是滾動的,因爲它是滾動視圖的背景。所以當滾動移動時,背景會移動。您可以使滾動視圖的背景透明(可能通過將背景顏色設置爲[UIColor clearColor]並將opaque設置爲NO),然後將視圖放在具有相同框架的滾動視圖後面。這不會隨滾動視圖一起移動。請記住,滾動視圖移動他們的內容,而不是自己。背景是該內容的一部分。

編輯:

更改此:

- (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)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. 
} 

然後是這樣的:

UIView *customScrollBackground = [[UIView alloc] initWithFrame:scrollView.frame]; 
customScrollBackground.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]]; 
[customScrollBackground addSubview:scrollView]; 
+0

,我想插入[UI顏色clearColor]在loadview或視圖中加載 – User123 2012-07-28 09:07:32

+0

self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@「bg-image.png」]];我使用這個評論也爲背景圖片...但爲此也滾動內容的背景移動.. – User123 2012-07-28 09:10:39

+0

看到更新的答案 – Marty 2012-07-28 09:15:49