Iphone 滚动背景

Iphone 滚动背景,iphone,cocoa-touch,Iphone,Cocoa Touch,在我的应用程序中,我正在向UITextView添加背景。但是当文本视图向下滚动时,图像与文本一起移动,新文本行的背景显示为白色。有没有一个简单的方法来处理它? 这是我的密码: textView = [ [UITextView alloc] initWithFrame: CGRectMake(10, 100, 270, 130)]; UIImageView *imgView = [ [UIImageView alloc]initWithFrame: CGRectMake(0, 0, 270, 1

在我的应用程序中,我正在向UITextView添加背景。但是当文本视图向下滚动时,图像与文本一起移动,新文本行的背景显示为白色。有没有一个简单的方法来处理它? 这是我的密码:

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

我希望背景保持静态,文本在其上方滚动。

如果希望背景保持静态,文本在其上方滚动,只需将UIImageView添加到要添加UIScrollView的同一视图中(具有相同的尺寸等)。 比如:

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];
我假设这段代码是在UIView上运行的,所以我添加了UIImageView本身。如果您是从UIViewController执行此操作,您可能需要将其更改为
[self.view addSubView:main scrollview]

谢谢pgb

这段代码终于对我有用了:

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

您希望背景在滚动时移动还是在文本滚动时保持静止?我希望背景在文本滚动时保持静止。