【问题标题】:ImageView in back of UITextView not clipping to boundsUITextView 后面的 ImageView 没有裁剪到边界
【发布时间】:2017-02-21 00:03:30
【问题描述】:

为了在我的UITextView 中获得背景图像,我在代码中创建了一个UIImageView,用我想要的图像填充它,将其添加为UITextViewsubView,并将其发送到背部。这适用于某些屏幕,但在其他屏幕上看起来像这样。

查看上面的UITextFields,了解它的外观。
这是我的代码。

let img = UIImageView(frame: self.bioTextView.bounds)
img.image = #imageLiteral(resourceName: "text_field_bio")
self.bioTextView.backgroundColor = UIColor.clear
self.bioTextView.addSubview(img)
self.bioTextView.sendSubview(toBack: img)
self.bioTextView.textContainerInset.left = 10

如何让图像的右侧剪辑到尾随边界,使其不会超出 UITextview。

编辑: 这是一个编辑,包括当我包含此代码时发生的情况的图像。

self.bioTextView.layer.borderWidth = 1
self.bioTextView.layer.borderColor = UIColor.red.cgColor

【问题讨论】:

  • bioTextView 的界限是什么?我的第一直觉是它比其他文本字段宽得多,因为它似乎剪裁了左侧。
  • bioTextView 与 TextFields 共享前沿和后沿
  • textviews也是代码添加的?可能是添加imageView时,textView的bounds不正确。
  • 文本视图没有添加到代码中。它们在 Storyboard 中布局,我用 IBOutlet 引用它们
  • @GabeSpound 你可以使用这个代码bioTextView.layer.borderWidth = 1bioTextView.layer.borderColor = UIColor.red.cgColor 并发布图像结果,看看发生了什么

标签: ios swift uiimageview uitextview


【解决方案1】:

@Zhang 的评论解决了我的问题。因为背景图像只是一个纯色,所以用myTextField.layer.cornerRadius = 10 圆角,用myTextField.clipsToBounds = true 将其裁剪到边界,然后用myTextField.layer.backgroundColor = UIColor 设置背景颜色会容易得多

【讨论】:

    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多