Search code examples
iosuitextviewtransparencyrounded-cornersuicontainerview

Rounded Corners of an Embedded UITextView – What Is (Incorrectly) Keeping the Corners Opaque?


NOTE: I edited this for clarity after figuring out the solution (below).

In interface builder, I embedded a UIView within another UIView and linked them to their respective UIViewControllers, EmbeddedViewController and MainViewController. Then I put a UITextView into the embedded UIView and created a reference to it in EmbeddedViewController.

In MainViewController I programmatically set view.backgroundColor = UIColor.lightGray. In the EmbeddedViewController I set textView.backgroundColor = UIColor.white and textView.layer.cornerRadius = 50. Using those colors, it appeared that the corners hadn't been rounded. But when I set textView.backgroundColor = UIColor.red, it was obvious that the rounding worked, but that the corners of something were opaque white. The problem is, I don't know what that something is, and after trying out several possibilities, I still don't know what I need to fix.

None of the prior questions seem to have dealt with this particular problem (i.e., non-transparency around the rounded corners of a UITextView in an embedded UIView), but if I missed something, please brow beat me for not trying hard enough. Otherwise, I'd be happy with either an interface builder or a programmatic solution.

Here's an image (per request), in which textView.backgroundColor = UIColor.red:

enter image description here


Solution

  • The problem was that I incorrectly thought that I had embedded a UITextView, when in fact I embedded a UIView that contained a UITextView. So instead of two views to deal with, there are actually three: the MainViewController's view, the EmbeddedViewController's view, and the EmbeddedViewController's textView. So to round the corners of the UITextView, here's a reasonable approach:

    In EmbeddedViewController, set view.layer.cornerRadius = 50 and set view.clipsToBounds = true. Since the UITextView is a subview of the embedded UIView, textView gets clipped to the bounds of view.

    With that, the above white regions are no more.