0

enter image description here

enter image description here

Hi I am developing small IOS application in which I am using scrollview with auto-layout.Inside scroll I am adding two more views. I am using IB and auto-layout constraints. I am adding two views in side one after another in vertical manner. I have added outer constraints like trailing, leading, top, bottom space. I also added height constraints for both views. Till this everything is working fine.

But my passengerView has some dynamic content. For that reason I want to make height constraint greater than equal to instead of equal to.

my code is:-

@IBOutlet weak var secondView: UIView!
@IBOutlet weak var scrollView: UIScrollView!

@IBOutlet weak var passengerView: UIView!

@IBOutlet weak var detailView: UIView!

override func viewDidLoad() {
        super.viewDidLoad()

let nameLabel = UILabel(frame: CGRect(x: 0, y: (self.name.frame.height * CGFloat(index) + CGFloat(49)), width: 161, height: 32))
                                    let idLabel = UILabel(frame: CGRect(x: 161, y: (self.name.frame.height * CGFloat(index) + CGFloat(49)), width: 161, height: 32))
                                   // nameLabel.text = dsdh?["name"] as? String
                                    nameLabel.layer.borderWidth = 1
                                    nameLabel.layer.borderColor = UIColor.black.cgColor
                                    nameLabel.textAlignment = .center;
                                    self.passengerView.addSubview(nameLabel)
                                    //idLabel.text = dsdh?["document_Type"] as? String
                                    idLabel.layer.borderWidth = 1
                                    idLabel.layer.borderColor = UIColor.black.cgColor
                                    idLabel.textAlignment = .center;
                                    self.passengerView.addSubview(idLabel)
                                    self.secondView.addSubview(self.passengerView)
                                    self.detailView.frame = CGRect(x: self.detailView.frame.origin.x, y: self.detailView.frame.origin.y + CGFloat(32) , width: self.detailView.frame.size.width, height: self.detailView.frame.size.height)
                                      self.secondView.addSubview(self.detailView)
                                    //self.scrollView.addSubview(self.detailView)
                                    self.secondView.frame = CGRect(x: self.secondView.frame.origin.x, y: self.secondView.frame.origin.y , width: self.secondView.frame.size.width, height: self.secondView.frame.size.height + CGFloat(5))

                                    self.scrollView.addSubview(self.secondView)
                                    //self.scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width , height: self.scrollView.frame.size.height + CGFloat(32))
                                    self.scrollView.frame = CGRect(x: self.scrollView.frame.origin.x, y: self.scrollView.frame.origin.y , width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height + CGFloat(0))
                                    self.view.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y , width: self.view.frame.size.width, height: self.view.frame.size.height + CGFloat(0))

}
Jaap
  • 81,064
  • 34
  • 182
  • 193

1 Answers1

0

The best way to do this would be as following:

  1. Implement a UIScrollView that is constraint to the Leading, Trailing, Top, Bottom constraints of your main view.
  2. Add a UIView to the UIScrollView and constrain it to the Leading, Trailing, Top, Bottom of your UIScrollView. Important is to also constrain this UIView's width to your main view, not your scroll view! In this way this UIView will serve you as a Content View for the UIScrollView. This step is mandatory if you are doing this in Interface Builder!
  3. Add a UIStackView to your Content View (the view from the previous step) and constrain it to the Leading, Trailing, Top, Bottom of the Content View.
  4. Add all your UILabels, your Detail View, QR Code View and Passenger View as children to this UIStackView.
  5. Since you want only your Passenger View to have a dynamic height you can set your UIStackView's axis to Vertical and Content Mode to Fill. Then you will provide a height for all of the children of the UIStackView except the Passenger View.

In this way the Content Size of your UIScrollView will adapt based on the height of the UIStackView and the height of the UIStackView willl adapt based on the needed height for the Passenger View.

Stefan Stefanov
  • 829
  • 7
  • 23
  • Hi Stefan, i tried with this. when i am adding the label into passengerView its going to beyond that and some of adding into below view. If you want me to share the code with you please send me mail-id. – aditya srivastava Feb 20 '17 at 06:39