2009-04-18 4 views
1

в моем приложении Я добавляю фон в UITextView. Но когда текстовое представление прокручивается вниз, изображение идет вместе с текстом, а фон для новых строк текста выглядит белым. Есть ли простой способ справиться с этим? Вот мой код:scrolling background

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

Я хочу, чтобы фон оставаться статичным с текстом прокрутки поверх него.

+0

Вы хотите, чтобы фон двигаться по мере прокрутки или оставаться статичным с текстом прокруткой поверх него? – pgb

+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. Если вы делаете это с UIViewController, вы можете изменить его на [self.view addSubView:mainScrollView].

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

textView.backgroundColor = [UIColor clearColor]; –