0

As per the image below, a custom control with an embedded UITextView will jump out of its bounds. I've tried applying clip to bounds etc. to no avail. Probably something simple when establishing a custom control.

Here is the code for the ViewController. Result is as follows

enter image description here

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var textStackView: UIStackView!
@IBOutlet weak var tabStackView: UIStackView!

override func viewDidLoad() {
    super.viewDidLoad()
    addButtons()
    addTextViews()
}

func addButtons(){
    var count = 0
    while (count <= 10){
        let btn = UIButton()
        btn.backgroundColor = UIColor.gray
        btn.setTitle("Btn \(count)", for: .normal)
        tabStackView.addArrangedSubview(btn)
        count += 1
    }
}

func addTextViews(){

    var count = 0
    while (count <= 5){

        if (count == 0){
            let textView = CustomView()
            textView.layer.borderColor = UIColor.green.cgColor //Green border
            textView.layer.borderWidth = 1
            textView.embededTextView.text = "Some sample text here. Some sample text here. Some sample text here. Some sample text here. Some sample text here."
            //textView.sizeToFit() //Does nothing
            //textView.clipsToBounds = true //Doesnt prevent overflow
            textStackView.addArrangedSubview(textView)
        }
        else{
            let textView = UITextView()
            textView.layer.borderColor = UIColor.black.cgColor
            textView.layer.borderWidth = 1
            textView.text = "Some sample text here. Some sample text here. Some sample text here. Some sample text here. Some sample text here."
            textStackView.addArrangedSubview(textView)
        }
        count += 1
    }
}
}

and the custom control

import UIKit

class CustomView: UIView {

@IBOutlet var view: UIView!
@IBOutlet weak var embededTextView: UITextView!

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)
    self.addSubview(self.view)
}

override init(frame: CGRect){
    super.init(frame: frame)
    Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)
    self.addSubview(self.view)
    view.layer.borderWidth = 4
    view.layer.borderColor = UIColor.red.cgColor
    //view.sizeToFit()
}
}
feedMe
  • 3,431
  • 2
  • 36
  • 61
Yarm
  • 1,178
  • 4
  • 16
  • 29
  • Please check how you are adding the views to the stack view. Can you please show what the expected output is in the form of a diagram – user1046037 Mar 13 '17 at 09:29
  • The aim was to not have the overflow. See answer below 'view.frame = self.bounds'. Thanks – Yarm Mar 13 '17 at 09:42

1 Answers1

0

What was missing was view.frame = self.bounds

in

override init(frame: CGRect){
    super.init(frame: frame)
    Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)
    view.frame = self.bounds
    view.layer.borderWidth = 4
    view.layer.borderColor = UIColor.red.cgColor
    self.addSubview(self.view)
}
Yarm
  • 1,178
  • 4
  • 16
  • 29