2017-04-23 54 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 
    } 

} 
相关问题