There is a UIView in it Label (price) and image (basket). If the price is 5 digits, then it does not fit the width of the UIView . I create everything programmatically and superimpose each other programmatically. Here is the code:
func customview(vc: UIViewController, num: String){ let v = UIView() v.frame.origin.x = 0.0 v.frame.origin.y = 0.0 v.frame.size.width = 78 v.frame.size.height = 30 v.backgroundColor = UIColor.whiteColor() v.layer.cornerRadius = 15 let touch = UITapGestureRecognizer(target:vc, action:Selector("boxButton:")) //v.autoresizingMask = [.FlexibleBottomMargin, .FlexibleTopMargin, .FlexibleLeftMargin, .FlexibleRightMargin] v.addGestureRecognizer(touch) let imgBox = UIImageView(image: UIImage(named: "box_image_gray.png")) imgBox.frame.origin.x = 50.0 imgBox.frame.origin.y = 5.0 imgBox.frame.size.width = 20 imgBox.frame.size.height = 20 imgBox.contentMode = UIViewContentMode.ScaleAspectFit v.addSubview(imgBox) let textview = UILabel() textview.frame.origin.x = v.frame.origin.x + 3 textview.frame.origin.y = 0.0 textview.frame.size.width = 50 textview.frame.size.height = 30 textview.text = num textview.font = UIFont(name:"HelveticaNeue-Bold", size: 13.0)//UIFont.systemFontOfSize(12) textview.textColor = UIColor(rgba: "#F22524") textview.textAlignment = NSTextAlignment.Center v.addSubview(textview) let logButton : UIBarButtonItem = UIBarButtonItem(customView: v) vc.navigationItem.rightBarButtonItem = logButton } How to make the UIView change the width depending on the length of the UILabel ?
I looked at the English-language forum, they write about LayoutConstraint , but unfortunately I did not understand how to use it.


