I've created a UICollectionView through the storyboard and temporarily positioned it in the view; however, I'd like to overwrite the positions programmatically on launch rather than using the storyboard configuration.
I thought viewDidLoad()
was the right location to place make this change?
Swift 2 Code:
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.backgroundColor = UIColor(white:1, alpha:0.0)
// Doesn't stretch the width to the device's width on launch. :-(
collectionView.frame.size = CGSize(width: UIScreen.mainScreen().bounds.width, height: 200)
}
If you are setting constraints and then want to change height
or width
then you should take outlet of that height or width and increament it's constant value. just ctrl+drag from width constraint to class and make outlate and then increment that outlate's contant property in viewdidload
.
for example,
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
self.heightConstraint.constant = 200
And second thing if you want to keep your collectionview's height equal with device then there is no need to set programatically. autolayout manage it if you set proper constraint. so main point is use autolayout at evrywhere or not at all. dont mix both autolayout and then set frame programatically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.