滚动背景

时间:2009-04-18 12:19:11

标签: iphone cocoa-touch

在我的应用中,我正在为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];

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

2 个答案:

答案 0 :(得分:2)

如果您希望背景保持静态并且文本在其上滚动,只需将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 self。如果您是从UIViewController执行此操作,则可能需要将其更改为[self.view addSubView:mainScrollView]

答案 1 :(得分:0)

谢谢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];
相关问题