2016-10-10 10 views
0

コレクションビューの上部にスティッキーヘッダーを作成したいので、スクロールしようとすると、ヘッダーが常に表示されます。フレームの代わりに自動レイアウトの制約を使用するには?

以下のコードが有効でした。

私は自動レイアウトにこの行を置き換えたいか
collectionView?.contentInset = UIEdgeInsets(top: 59, left: 0, bottom: 0, right: 0) 

if let width = collectionView?.bounds.width { 
    let heaaderEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) 
    heaaderEffectView.frame = CGRectMake(0, 64, width, 59) 

    let headerView = DetailedReviewsHeader(frame: CGRectMake(0, 0, width, 59)) 
    heaaderEffectView.contentView.addSubview(headerView) 
    headerView.numberOfReviews = reviews?.count ?? 0 
    headerView.sortButton.addTarget(self, action: #selector(showSortOptions(_:)), forControlEvents: .TouchUpInside) 

    view.addSubview(heaaderEffectView) 
} 

heaaderEffectView.frame = CGRectMake(0, 64, width, 59) 

私は

view.addSubview(headerEffectView) 
    headerEffectView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true 
    headerEffectView.heightAnchor.constraintEqualToConstant(59.0).active = true 

を試みたが、それは動作しません。フレームは常に(0,0,0,0)です。何か案は ?

答えて

0

制約を適用するコードを追加しました。ここにあなたの要件のコードです。一度試してみてください。行末のコメントをはっきりと見てください。

collectionView?.contentInset = UIEdgeInsets(top: 59, left: 0, bottom: 0, right: 0) 

if let width = collectionView?.bounds.width { 
    let heaaderEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) 
    //heaaderEffectView.frame = CGRectMake(0, 64, width, 59) 

    view.addSubview(heaaderEffectView) 
    heaaderEffectView.translatesAutoresizingMaskIntoConstraints = false 

    heaaderEffectView.addConstraint(NSLayoutConstraint(item: heaaderEffectView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: width))// for width of heaaderEffectView 
    heaaderEffectView.addConstraint(NSLayoutConstraint(item: heaaderEffectView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 59.0))// for height of heaaderEffectView 
    view.addConstraint(NSLayoutConstraint(item: heaaderEffectView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 64.0))// for y spacing that is top constraint to super view from heaaderEffectView 
    view.addConstraint(NSLayoutConstraint(item: heaaderEffectView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))// for x spacing that is leading constraint to super view from heaaderEffectView 

    let headerView = DetailedReviewsHeader(frame: CGRectMake(0, 0, width, 59))// you can remove using frame but i kept it because i don't know what the initializer does with the frame 
    headerView.numberOfReviews = reviews?.count ?? 0 
    headerView.sortButton.addTarget(self, action: #selector(showSortOptions(_:)), forControlEvents: .TouchUpInside) 

    heaaderEffectView.contentView.addSubview(headerView) 
    headerView.translatesAutoresizingMaskIntoConstraints = false 

    heaaderEffectView.addConstraint(NSLayoutConstraint(item: headerView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: heaaderEffectView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))// for leading 0 
    heaaderEffectView.addConstraint(NSLayoutConstraint(item: headerView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: heaaderEffectView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))// for trailing 0 
    heaaderEffectView.addConstraint(NSLayoutConstraint(item: headerView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: heaaderEffectView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))// for top 0 
    heaaderEffectView.addConstraint(NSLayoutConstraint(item: headerView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: heaaderEffectView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))// for bottom 0 

    view.layoutIfNeeded() 
} 
+0

はちょっと感謝:)最後に私が使用していますheaderEffectView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchorは、定数は:0) –

+0

真= .activeは、それがあまりにもうまく動作します。しかし、多くのおかげで:) –

+0

私は私のミスは、私は制約と再生する前にaddsubviewsていないと思う:) –

関連する問題