2016-09-19 28 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) 
相关问题