Title: how to wrap content a custom view in Swift
Tags: swift;xib
Question: I have created a xib file which contains some subviews. I set the appreciate  width and height for the subviews.
there is class which inherited UIView. This class name is CustomUIView. Then I set This class as the file owner of that xib file.
Now I want to add this xib file to a view controller in storyboard. so I add a UIView and set its class to CustomUIView.
But I have to set width and height for this view. I set leading and top constraint. but I want the height and width of it be set based on the height and width of the subviews.
How should I do it
Comment: @user1046037 Thanks. but I set these in the subviews of the custom view and I don't want to set them again.

Comment: Set leading and trailing constraint to determine the width. Set top and bottom constraint to determine the height. Please read about Autolayout


Here is another answer: You don't have to set width and height anchors for the view , if you already set it inside the xib for every element
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let v = (Bundle.main.loadNibNamed("hhhh", owner: self, options: nil))?[0] as! hhhh;
view.addSubview(v)
v.translatesAutoresizingMaskIntoConstraints = false
v.topAnchor.constraint(equalTo:self.view.topAnchor,constant:30).isActive = true
v.leadingAnchor.constraint(equalTo:self.view.leadingAnchor,constant:30).isActive = true
Result
Comment for this answer: Thanks. Exactly. I needed it to do so. but when I set tap gesture recognizer, only when I assign width and height it works. otherwise it doesn't work. and auto layout shows error that it needs width and height for that view

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:07/29/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<swift><xib>', 'Title': 'how to wrap content a custom view in Swift', 'ViewCount': 1318, 'id': 51580017, 'user_AboutMe': '', 'user_AccountId': 4846352.0, 'user_DisplayName': 'Fattaneh Talebi', 'user_Reputation': 657.0, 'user_Views': 349.0, 'user_id': 4468859.0}}