swift 8 iOS数据在滚动视图中进行裁剪,同时添加动态数据

时间:2017-02-18 06:57:47

标签: ios swift dynamic uiview scrollview

enter image description here

enter image description here

您好我正在开发小型IOS应用程序,其中我正在使用带有自动布局的scrollview。内部滚动我正在添加两个视图。我正在使用IB和自动布局约束。我以纵向方式一个接一个地添加两个视图。我添加了外部约束,如尾随,前导,顶部,底部空间。我还为两个视图添加了高度限制。直到这一切都运转良好。

但我的passengerView有一些动态内容。出于这个原因,我想使高度约束大于等于而不是等于。

我的代码是: -

@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))

}

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法如下:

  1. 实施一个约束主视图UIScrollView约束的Leading, Trailing, Top, Bottom
  2. UIView添加UIScrollView,并将其约束到Leading, Trailing, Top, Bottom的{​​{1}}。重要的是还要将此UIScrollView宽度限制在主视图中,而不是滚动视图!这样,UIView's将为UIView提供Content View UIScrollView如果您在Interface Builder中执行此操作,则必须执行此步骤
  3. UIStackView(上一步中的视图)中添加Content View,并将其约束到Leading, Trailing, Top, Bottom的{​​{1}}。
  4. 将所有Content View,您的UILabelsDetail ViewQR Code View作为孩子添加到此Passenger View
  5. 由于您只希望UIStackView具有动态高度,因此您可以将Passenger View轴设置为UIStackView's,将Vertical设置为Content Mode。然后,除了Fill 之外,您将为UIStackView 的所有孩子提供一个高度。
  6. 通过这种方式,Passenger View的{​​{1}}将根据Content Size的高度进行调整,UIScrollView的高度将根据所需的高度进行调整UIStackView