2016-10-10 72 views
0

我想在collectionView的頂部創建一個粘性標題,所以當我嘗試滾動時,標題將始終存在。如何使用自動佈局約束而不是框架?

下面的代碼是作品。

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

和它的作品也很好。但非常感謝:) –

+0

我認爲我的錯誤是我在播放約束之前沒有addsubviews :) –