enter image description here

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.

  • You can try to change the frame itself, or if there are constrintells, then do it on the property width and change its size. - Orest Mykha

1 answer 1

The problem is that you rigidly set the size of the frame, that does not fit

For example, you can jot down everything in xib and then use. Or prescribe the width in the code depending on the length of the text. Or programmatically paint where some constraints. An example of your case:

GitHub Example

Swift 3 :

 func customview(vc: UIViewController, num: String){ let v = UIView() v.backgroundColor = UIColor.white v.layer.cornerRadius = 15 //let touch = UITapGestureRecognizer(target:vc, action:Selector("boxButton:")) //v.addGestureRecognizer(touch) let imgBox = UIImageView(image: UIImage(named: "box_image_gray.png")) imgBox.contentMode = UIViewContentMode.scaleAspectFit v.addSubview(imgBox) let textview = UILabel() textview.text = num textview.font = UIFont(name:"HelveticaNeue-Bold", size: 13.0)//UIFont.systemFontOfSize(12) textview.textColor = UIColor.black textview.textAlignment = NSTextAlignment.center v.addSubview(textview) v.translatesAutoresizingMaskIntoConstraints = false textview.translatesAutoresizingMaskIntoConstraints = false imgBox.translatesAutoresizingMaskIntoConstraints = false //в Swift 2 NSLayoutConstraint.constraintsWithVisualFormat(... v.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-5-[img(20)]-5-|", options: [], metrics: nil, views: ["img": imgBox])) v.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[lbl(30)]-0-|", options: [], metrics: nil, views: ["lbl": textview])) v.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-5-[lbl(>=50)]-5-[img(20)]-5-|", options: [], metrics: nil, views: ["img": imgBox, "lbl": textview])) NSLayoutConstraint.activate(v.constraints) let logButton : UIBarButtonItem = UIBarButtonItem(customView: v) vc.navigationItem.rightBarButtonItem = logButton } 

result:

enter image description here enter image description here

  • Very elegant example, thank you, but unfortunately the view does not look like it hangs on the rightBarButtonItem, with the build it hangs from the left. - Kerim Khasbulatov
  • @IsmailHasbulatov can make a minimal project where this problem is reproduced? Updated the answer with his example. - VAndrJ
  • Unloaded on GitHub, here is the link github.com/muaviya/ResizeView.git - Kerim Khasbulatov
  • @Ismail Khasbulatov launched, the button on the right. If you left, then a very interesting option is obtained. - VAndrJ