2016-09-19 73 views
0

我想將我的collectionView放置在我的分段控件下方,但是我不知道是否我的約束錯誤。我想也將我的分段控件填充整個框架寬度減去十,但集中在collectionView上方。UICollectionView以編程方式位置和分段控制錯誤

繼承人的代碼,我一直在努力:

let item = ["Past", "Future"] 
    let customSC = UISegmentedControl(items: item) 

    customSC.selectedSegmentIndex = 0 
    let frame = UIScreen.mainScreen().bounds 
    customSC.frame = CGRectMake(0, 0, 
           frame.width, 25) 

    customSC.layer.cornerRadius = 5.0 // Don't let background bleed 
    customSC.backgroundColor = MaterialColor.white 
    customSC.tintColor = MaterialColor.red.accent3 

    customSC.translatesAutoresizingMaskIntoConstraints = false 
    //customSC.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor) 
    //collectionView?.translatesAutoresizingMaskIntoConstraints = false 
    collectionView?.addSubview(customSC) 
    customSC.centerXAnchor.constraintEqualToAnchor(collectionView?.centerXAnchor).active = true 
    //collectionView?.topAnchor.constraintEqualToAnchor(customSC.bottomAnchor,constant: 2).active = true 

這裏是我輸出的圖像。我將我的分段視圖作爲我的第一個視圖來填充整個寬度框架和我的集合視圖,以顯示在我的分段控件下方。

enter image description here

感謝。

回答

0

您需要在當前視圖不是Collection視圖添加CustomSc。做類似這樣的

collectionView.frame = CGRectMake(0, customSC.frame.origin.y + 25, self.view.frame.size.width, set Acc. to your req.) 

self.view.addSubview(customSC) 

集的CollectionView框架現在在當前視圖中添加的CollectionView這樣

self.view.addSubview(collectionView)