views:

619

answers:

4

Hi, in my app I am adding a background to UITextView. But when the text view scrolls down the image goes together with the text and the background for new lines of text appears to be white. Is there an easy way to cope with it? Here is my code:

textView = [ [UITextView alloc] initWithFrame: CGRectMake(10, 100, 270, 130)];

UIImageView *imgView = [ [UIImageView alloc]initWithFrame: CGRectMake(0, 0, 270, 130)];
imgView.image = [UIImage imageNamed: @"background.png"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
[imgView release];


textView.opaque = YES;
textView.editable = YES;
textView.font = [UIFont systemFontOfSize: 12];
textView.textColor = [UIColor colorWithRed: 10.0f/255.0f green: 10.0f/255.0f blue: 10.0f/255.0f alpha: 1.0f];
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
textView.delegate = self;

[mainScrollView addSubview: textView];

I want the background to remain static with the text scrolling on top of it.

A: 

I'm not sure, but I think you should not be adding the image view to the scroll view, instead set the text and scroll views to have transparent backgrounds and place the image view behind both.

Mike Broughton
+2  A: 

If you want the background to remain static and have the text scroll on top of it, just add the UIImageView to the same view you are adding the UIScrollView to (with same dimensions, etc). Something like:

UIImageView *imgView = [ [UIImageView alloc]initWithFrame: CGRectMake(0, 0, 270, 130)];
imgView.image = [UIImage imageNamed: @"background.png"];
[self addSubView:imgView];
[imgView release];

textView = [ [UITextView alloc] initWithFrame: CGRectMake(10, 100, 270, 130)];

textView.opaque = YES;
textView.editable = YES;
textView.font = [UIFont systemFontOfSize: 12];
textView.textColor = [UIColor colorWithRed: 10.0f/255.0f green: 10.0f/255.0f blue: 10.0f/255.0f alpha: 1.0f];
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
textView.delegate = self;
// Make the background of the textView transparent
textView.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0];

[mainScrollView addSubview: textView];

I'm assuming this code is run on a UIView, so I'm adding both the UIImageView self. If you are doing this from a UIViewController you may want to change it to [self.view addSubView:mainScrollView].

pgb
A: 

Thank you pgb.

This code finally works for me:

    UIImageView *imgView = [ [UIImageView alloc]initWithFrame: CGRectMake(10, 100, 270, 130)];
    imgView.image = [UIImage imageNamed: @"background.png"];
    [mainScrollView addSubview: imgView];
    [mainScrollView sendSubviewToBack: imgView];
    [imgView release];

    textView = [ [UITextView alloc] initWithFrame: CGRectMake(10, 100, 270, 130)];

    textView.opaque = YES;
    textView.editable = YES;
    textView.font = [UIFont systemFontOfSize: 12];
    textView.textColor = [UIColor colorWithRed: 10.0f/255.0f green: 10.0f/255.0f blue: 10.0f/255.0f alpha: 1.0f];
    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textView.delegate = self;
    // Make the background of the textView transparent
    textView.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0];

    [mainScrollView addSubview: textView];
Ilya
A: 

pgb, I have a similar issue, but I would like for my background image to scroll with the text does, much like Notepad works on the iPhone.

Jamplee

Jamplee
I recommend you post a new question instead, Stack Overflow doesn't work like a forum, and you will only get noticed if you post a new question.
pgb