'UIButton position to right side of UITextView in a growing textView

I have a UIButton() in a textView.

let expanButtonHeight = bounds.size.height
     expanButton.frame = CGRect(x: textView.bounds.size.width - expanButtonHeight, y: 0, width: expanButtonHeight, height: expanButtonHeight)

     self.addSubview(expanButton)
     self.layoutIfNeeded()

But when I type text in my textView the expanButton doesn't change. How do I keep the UIButton position fixed at right side of UITextView even when textView is growing in width?

This is the problem I'm facing: This is the problem I'm facing

I want the button to stay at end of textView: I want the button to stay at end of textView



Solution 1:[1]

With what you wish, I suggest you to have a UIView as a parent container of UITextView and UIButton. Your button should add to UIView instead of adding to UITextView. if the UITextView grow, make the parent container UIView grow as well and you can stick your UIButton at any place you want based on UIView.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sereysophea Eap