3

UITextView在我的 iPhone 应用程序中有。在UITextView i have added UIImageView as subview. 当输入的文本到达final height the texts are scrolling to top. 取而代之的是UIImageView (with image) also scrolling top. 我该如何处理以停止图像滚动并允许文本滚动?这是我的代码供您参考,

    messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
    messageTextView.delegate = self;
    messageTextView.backgroundColor = [UIColor clearColor];
    messageTextView.clipsToBounds = YES;  
    messageTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
    messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    textViewImageView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 30)];
    textViewImageView.image = [UIImage imageNamed: @"textbg.png"];
    textViewImageView.contentMode    = UIViewContentModeScaleToFill;
    textViewImageView.contentStretch = CGRectMake(0.5, 0.5, 0, 0);  
    [messageTextView addSubview: textViewImageView];

    [messageTextView sendSubviewToBack: textViewImageView];
    [messageTextView addSubview:textViewLabel];

谁能帮我解决这个问题?提前致谢。期待您的回复。

在此处输入图像描述

4

3 回答 3

3

您是否考虑过将 imageView 放在 textView 后面?或者有什么理由禁止这样的布局?

类似的东西:

UITextView *messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:messageTextView];

UIImageView *textViewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
textViewImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:textViewImageView];

[self.view sendSubviewToBack:textViewImageView];
于 2012-09-26T00:31:30.477 回答
2

也许这会有所帮助。当图像到达边界时,使用滚动视图委托重新定位图像。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {

        CGRect imageFrame = textViewImageView.frame;
        imageFrame.origin.y = scrollView.contentOffset.y - (scrollView.contentSize.height - scrollView.frame.size.height);
        textViewImageView.frame = imageFrame;

    }       
}
于 2012-09-25T14:42:15.993 回答
0

这应该有效:

[yourTxtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"your image.png"]]];
于 2012-09-25T14:31:01.573 回答