简体   繁体   中英

UITextView subview UIImageView hiding entered text and scrolling with texts iPhone?

I have UITextView in my iPhone app. In the UITextView i have added UIImageView as subview . When the entered text reaches the final height the texts are scrolling to top . Instead the UIImageView (with image) also scrolling top . How can i handle to stop the image scroll and allow the text to scroll? Here is my code for your reference,

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

Can anyone please help me to solve this? Thanks in advance. Looking forward your reply.

在此输入图像描述

have you considered to position the imageView behind the textView? Or is there any reason that forbids such a layout?

something like that:

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

Maybe this could help. Use the scrollview delegate to reposition your image when it reaches the bounds.

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

    }       
}

这应该工作:

[yourTxtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"your image.png"]]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM