我對iOS很陌生並且還不太瞭解約束條件。我將一組動態視圖添加到我的視圖控制器,默認情況下,它包含頂部的滾動視圖和容器視圖。一切工作正常,我是管理使用,將它們添加下面互相以下:UIView以編程方式設置約束條件
TheView.frame = CGRectMake(5, CGFloat(position), self.view.frame.size.width-10, 40); //position is incremented so that they are below each other
但是,當我打開手機,以景觀,意見不採取主視圖的寬度,使我增加了以下各方面的意見:
TheView.translatesAutoresizingMaskIntoConstraints = false;
let margins = contentView.layoutMarginsGuide // contentView is the container view
TheView.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).active = true
TheView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor).active = true
此提出的意見動態地改變它們的寬度,當我旋轉設備,但他們現在重疊和我似乎無法找出如何我可以將它們放置低於對方喜歡他們是怎麼樣的。
UPDATE:
我除去滾動視圖和一切從故事板和編程添加滾動視圖和堆棧視圖。
在viewDidLoad中:
ScrollView = UIScrollView()
ScrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(ScrollView)
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: .AlignAllCenterX, metrics: nil, views: ["scrollView": ScrollView]))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: .AlignAllCenterX, metrics: nil, views: ["scrollView": ScrollView]))
stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.distribution = UIStackViewDistribution.EqualSpacing
stackView.spacing = 26.0
stackView.axis = .Vertical
ScrollView.addSubview(stackView)
ScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[stackView]|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
ScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[stackView]|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
添加動態的觀點:
for loop...
let Description = UILabel()
stackView.addArrangedSubview(Description)
let Value = UITextField()
stackView.addArrangedSubview(Value)
的視圖添加成功如前且有滾動。視圖也正在填充堆棧視圖(寬度),但仍不在橫向。
在UIVewController級別也可以處理風景方向 - 請參閱「supportedInterfaceOrientation」 - 並在您的應用程序級別(請參閱您的應用程序目標上支持的方向)當您轉爲風景時,屏幕是否會隨着狀態欄一起旋轉? – Vinzzz
抱歉延遲迴復..是的,但我不明白這是如何相關的。支持的界面方向是肖像,風景左和右 –