'UIButton position to bottom right corner of textView in swift

I have a custom class UIView in which i have a textView. And I'm trying to add UIButton to bottom right corner of textView programmatically using autoresizingMask.This is my code:

public init() {
textViewButton = UIButton(type: .custom)
     textViewButton.addTarget(self, action: #selector(didPressButton), for: .touchUpInside)
     textViewButton.autoresizingMask = [.flexibleTopMargin, .flexibleRightMargin, .flexibleLeftMargin]
     self.addSubview(textViewButton)
     textViewButton.isHidden = true
}

And I'm setting frame in layoutSubviews:

override public func layoutSubviews() {
     super.layoutSubviews()
textViewButton.frame = CGRect(x: bounds.maxX - 50, y: 0, width: 50, height: 50)
}

This is the current position of the button with my code: This is the current position with empty text This is the current position with my code

How do I fix the button to bottom right corner of textView?



Sources

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

Source: Stack Overflow

Solution Source