2017-04-23 52 views
0

我正在創建一個容器視圖,我希望它的容器的寬度是視圖寬度的3/4,並且我希望它的高度容器的單元高度是3/4。訪問UIView內的UICollectionView單元格和視圖框架(swift 3)

到目前爲止,我有什麼的看法是......

let messageContainer: UIView = { 
    let mContainer = UIView() 

    mContainer.frame = CGRect(x: 0, y: 0, width: (view.frame.width/4), height: (cell.frame.width/4)) 

    mContainer.backgroundColor = UIColor.clear 


    return mContainer 
}() 

當我試圖訪問它說他們是懸而未決的標識符的幀。

我知道如果有什麼,這可能是因爲messageContainer視圖位於類中。

有沒有什麼辦法可以使幀全局變量,以便我可以在任何地方訪問它們?

class FriendsController: UICollectionViewController, UICollectionViewDelegateFlowLayout, NSFetchedResultsControllerDelegate { 

class ShareCell: BaseCell { 

let containerView: UIView = { 
    let container = UIView() 
    container.backgroundColor = UIColor.clear 
    return container 
}() 

let messageContainer: UIView = { 
let mContainer = UIView() 

mContainer.frame = CGRect(x: 0, y: 0, width: (view.frame.width/4), height: (cell.frame.width/4)) 

mContainer.backgroundColor = UIColor.clear 


return mContainer 
}() 


fileprivate func setupShareCellView() { 

addSubview(containerView) 
containerView.addSubview(messageContainer) 

     //ContainerView Constraints 
    addConstraintsWithFormat("H:|[v0]|", views: containerView) 
    addConstraintsWithFormat("V:|[v0]|", views: containerView) 

} 
} 
} 

如何引用這些值?

有什麼建議嗎?

回答

0

創建容器視圖時不要設置框架。等到它被添加後。另外,使用錨點。他們更容易合作。

extension UIView { 

    func addThreeQuatersView(_ view: UIView) -> Void { 
     view.removeFromSuperview() // Just in case, but shouldn't do anything. 
     view.translatesAutoresizingMaskIntoConstraints = false 
     self.addSubview(view) 
     view.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.75).isActive = true 
     view.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.75).isActive = true 
    } 

} 
相關問題