2016-09-28 53 views
0

所以我一直試圖用約束以編程方式將UIVIew添加到UIScrollView。我只是沒有在模擬器中看到UIView。 我有一個IBOutlet的UIScrollview ...所以我不知道爲什麼我遇到這個問題。未能嘗試以編程方式在滾動視圖中添加UIView約束在SWIFT中

override func viewDidAppear(animated: Bool) { 

let ownerUIView:UIView! = UIView() 
     var innerLabel:UILabel = UILabel() 
     ownerUIView.translatesAutoresizingMaskIntoConstraints = false 
     innerLabel.translatesAutoresizingMaskIntoConstraints = false 
     innerLabel.text = "Test text" 
     ownerUIView.addSubview(innerLabel) 
     importantScroll.addSubview(ownerUIView) 
     ownerUIView.backgroundColor = UIColor.blueColor() 

     let constraint1 = NSLayoutConstraint(item: ownerUIView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: importantScroll, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 10) 
     let constraint2 = NSLayoutConstraint(item: ownerUIView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: importantScroll, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 10) 
     let constraint3 = NSLayoutConstraint(item: ownerUIView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 60) 
     let constraint4 = NSLayoutConstraint(item: ownerUIView, attribute: .Top , relatedBy: .Equal, toItem: importantScroll, attribute: NSLayoutAttribute.Top , multiplier: 1, constant: 20) 

     NSLayoutConstraint.activateConstraints([constraint1,constraint2,constraint3,constraint4]) 

} 

我刪除了約束,仍然無法看到UILabel或UIVIew。如果我刪除UILable,那麼我會看到藍色的UIView。

let ownerUIView:UIView = UIView(frame: CGRect(x: 10, y: 10, width: 200, height: 200)) 
     let innerLabel:UILabel = UILabel(frame: CGRect(x: 10, y: 10, width: 100, height: 100)) 
     ownerUIView.translatesAutoresizingMaskIntoConstraints = false 
     innerLabel.translatesAutoresizingMaskIntoConstraints = false 
     innerLabel.text = "Test text" 
     ownerUIView.addSubview(innerLabel) 
     importantScroll.addSubview(ownerUIView) 
     ownerUIView.backgroundColor = UIColor.blueColor() 

回答

0

我認爲你需要設置你的ownerView和importantScoll之間寬度相等約束,因爲你的滾動視圖無法確定內容的大小,直到沒有任何適當的寬度約束。你可以添加這一行。

let constraint5 = NSLayoutConstraint(item: ownerUIView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: importantScroll, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0.0) 
NSLayoutConstraint.activateConstraints([constraint1,constraint2,constraint3,constraint4,constraint5]) 

更新: 這是很常見的佈局在滾動型多子視圖時加入了滾動的內另一個容器的UIView。所以,我首先在Storyboard中添加了容器UIView。 ContainerView對scrollView具有相同的寬度和高度約束,並且我還添加了四個約束來將containerView約束到ScrollView。最後,我按照如下方式在體裁上設定了約束條件。

let constraint6 = NSLayoutConstraint(item: ownerView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: containerView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 40) 
let constraint7 = NSLayoutConstraint(item: containerView, attribute: .Tailing, relatedBy: .Equal, toItem: ownerView, attribute: .Trailing, multiplier: 1, constant: 10) 
let constraint8 = NSLayoutConstraint(item: ownerView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 60) 
let constraint9 = NSLayoutConstraint(item: ownerView, attribute: .Top , relatedBy: .Equal, toItem: containerView, attribute: NSLayoutAttribute.Top , multiplier: 1, constant: 20) 

containerView.addConstraints([constraint6,constraint7,constraint8,constraint9]) 
+0

嗨那裏....謝謝你...有沒有辦法讓UIView的右邊到10邊距? – BostonMacOSX

+0

是否有任何理由以編程方式設置約束?如果您在故事板中應用自動佈局,它將會非常簡單。你可以參考這個[link](https://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/)。 – woogii

+0

@BostonMacOSX我更新了我的答案。 – woogii

相關問題