ios - Update UICollectionView Width on Device Rotate in Swift -


i have custom keyboard uicollectionview. cells not have defined size. when rotating device portrait landscape keyboard resizes appropriately uicollectionview stays @ original width.

what best way tackle updating width expand full available with? how can update constraints available space?

the uicollectionview created programmatically , i'm not using storyboard implement autolayout way.

override func viewdidappear(animated: bool) {     let layout: uicollectionviewflowlayout = uicollectionviewflowlayout()     layout.sectioninset = uiedgeinsets(top: 5, left: 5, bottom: 5, right: 5)      collectionview = uicollectionview(frame: self.view.frame, collectionviewlayout: layout)      collectionview.datasource = self     collectionview.delegate = self      collectionview.registernib(uinib(nibname: "collectionviewcell", bundle: nil), forcellwithreuseidentifier: kbreuseidentifier)     collectionview.backgroundcolor = uicolor.whitecolor()     self.collectionview.translatesautoresizingmaskintoconstraints = true      self.view.addsubview(collectionview)      let collectionviewbottomconstraint = nslayoutconstraint(item: self.collectionview, attribute: .bottom, relatedby: .equal, toitem: self.view, attribute: .bottom, multiplier: 1.0, constant: -40.0)     let collectionviewtopconstraint = nslayoutconstraint(item: self.collectionview, attribute: .top, relatedby: .equal, toitem: self.view, attribute: .top, multiplier: 1.0, constant: 10)     let collectionviewleftconstraint = nslayoutconstraint(item: self.collectionview, attribute: .leadingmargin, relatedby: .equal, toitem: self.view, attribute: .leadingmargin, multiplier: 1.0, constant: 0)     let collectionviewrightconstraint = nslayoutconstraint(item: self.collectionview, attribute: .trailingmargin, relatedby: .equal, toitem: self.view, attribute: .trailingmargin, multiplier: 1.0, constant: 0)       self.view.addconstraints([collectionviewbottomconstraint, collectionviewtopconstraint, collectionviewrightconstraint, collectionviewleftconstraint])   } 

you need implement uicollectionviewdelegateflowlayout protocol on view controller.

on viewdidload, register orientation changed notifications:

nsnotificationcenter.defaultcenter().addobserver(self, selector: "orientationdidchange:", name: uideviceorientationdidchangenotification, object: nil) 

don't forget remove observer when viewcontroller closed:

deinit {     nsnotificationcenter.defaultcenter().removeobserver(self) } 

when orientation changes, tell collectionview update layout:

func orientationdidchange(notification: nsnotification) {     collectionview!.collectionviewlayout.invalidatelayout() } 

finally, implement uicollectionviewdelegateflowlayout protocol method. on method can calculate size collection view cells needed, referencing collectionview frame width or device screen size:

func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize {     // leave 10px margin on each side, plus 10px between columns     let columns = 2     let margings = 20 + 10 * (columns - 1)     let width = (collectionview.frame.size.width - margings) / columns     let height = width // square cells     return cgsizemake(width, height) } 

in case, want cells proportional portrait screen ratio. have variable number of columns, might not case.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -