【问题标题】:UITextView subview UIImageView hiding entered text and scrolling with texts iPhone?UITextView 子视图 UIImageView 隐藏输入的文本并滚动文本 iPhone?
【发布时间】:2012-09-25 14:20:19
【问题描述】:

我的 iPhone 应用中有UITextView。在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];

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

【问题讨论】:

  • 为什么不将该图像设置为 UITextField 背景?
  • 你的意思是添加 UITextView 作为 UIImageView 的子视图我的理解是正确的?

标签: iphone ios uiimageview uitextview subview


【解决方案1】:

您是否考虑过将 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];

【讨论】:

    【解决方案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;
    
        }       
    }
    

    【讨论】:

    • 我没有使用 UIScrollView。请问有什么建议吗?
    • UITextView 继承自 UIScrollView 所以这应该仍然有效。只要您设置 textview 委托。如果您有任何问题,请告诉我。
    • 这根本行不通。这是正确的概念,但改变 origin.y 是不够的......它会使视图表现得非常疯狂......(记住还有其他限制影响它的外观)
    • @Radu 你可能是对的。这曾经在 iOS 6 时代工作。可能不再是最好的解决方案了。也许现在使用普通约束可以解决这个问题?
    【解决方案3】:

    这应该可行:

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

    【讨论】:

    • 感谢您的回答。但我的图像尺寸太短于 uitextview 的宽度和高度。因此,当我添加 setBackgroundColor 时,它会显示多个图像。你能提出一些建议来解决这个问题吗?谢谢。
    • 我认为你的 UITextView 应该有一些固定的大小,和你的背景图片大小一样。如果您发布屏幕截图会更容易...
    • 我添加了一个屏幕截图供您参考。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多