ios - Auto Layout using layout anchors programmatically not working -
i trying add stack view containing uilabel @ top , uicollectionview underneath. trying constrain stack view takes full view, anchoring sides. when run app uilabel appears , slice of collection view appears. collection view says width , height both zero. appreciated, thank you.
var collectionview: uicollectionview! var titlelabel: uilabel! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. self.view.translatesautoresizingmaskintoconstraints = false let margins = self.view.layoutmarginsguide let layout: uicollectionviewflowlayout = uicollectionviewflowlayout() layout.sectioninset = uiedgeinsets(top: 20, left: 10, bottom: 10, right: 10) collectionview = uicollectionview(frame: self.view.frame, collectionviewlayout: layout) layout.itemsize = cgsize(width: self.collectionview.frame.width / 4, height: self.collectionview.frame.width / 4) layout.minimuminteritemspacing = self.collectionview.frame.width / 15 layout.minimumlinespacing = self.collectionview.frame.width / 5 collectionview.backgroundcolor = uicolor.black collectionview.datasource = self collectionview.delegate = self collectionview.register(uicollectionviewcell.self, forcellwithreuseidentifier: "cell") titlelabel = uilabel() titlelabel.translatesautoresizingmaskintoconstraints = false titlelabel.textalignment = .center titlelabel.numberoflines = 1 titlelabel.font = uifont.systemfont(ofsize: 22) titlelabel.backgroundcolor = uicolor.lightgray titlelabel.text = "challenges" titlelabel.textcolor = uicolor.red let stackview = uistackview(arrangedsubviews: [titlelabel, collectionview]) stackview.backgroundcolor = uicolor.white stackview.axis = .vertical stackview.distribution = .fillequally stackview.alignment = .fill stackview.spacing = 5 self.view.addsubview(stackview) stackview.translatesautoresizingmaskintoconstraints = false //stackview.centerxanchor.constraint(equalto: self.view.centerxanchor).isactive = true //stackview.centeryanchor.constraint(equalto: self.view.centeryanchor).isactive = true stackview.topanchor.constraint(equalto: self.view.topanchor).isactive = true stackview.leadinganchor.constraint(equalto: self.view.leadinganchor).isactive = true stackview.trailinganchor.constraint(equalto: self.view.trailinganchor).isactive = true stackview.bottomanchor.constraint(equalto: self.view.bottomanchor).isactive = true stackview.heightanchor.constraint(equalto: self.view.heightanchor, multiplier: 1.0).isactive = true }
update:
i took code , ran in new single view application , worked expected. because of think worth mention trying incorporate sprite kit game , present view controller doing this:
let root = self.view?.window?.rootviewcontroller var viewc = selectioncollectionviewcontroller() root?.present(viewc, animated: false, completion: nil)
are there special steps need take because done sprite kit?
i believe below screen shot expected output.
you might not need uistackview , can directly add self.view.
once added self.view, can set constraints. can print item size in viewdidlayoutsubviews
class viewcontroller: uiviewcontroller { var collectionview: uicollectionview! var titlelabel: uilabel! let collectioncellidentifier:string = "collectioncellid" override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. let layout: uicollectionviewflowlayout = uicollectionviewflowlayout() layout.sectioninset = uiedgeinsets(top: 20, left: 10, bottom: 10, right: 10) collectionview = uicollectionview(frame: cgrect.zero, collectionviewlayout: layout) collectionview.delegate = self collectionview.datasource = self collectionview.register(uicollectionviewcell.classforcoder(), forcellwithreuseidentifier: collectioncellidentifier) collectionview.backgroundcolor = uicolor.black collectionview.datasource = self collectionview.delegate = self collectionview.register(uicollectionviewcell.self, forcellwithreuseidentifier: "cell") titlelabel = uilabel() titlelabel.textalignment = .center titlelabel.numberoflines = 1 titlelabel.font = uifont.systemfont(ofsize: 22) titlelabel.backgroundcolor = uicolor.lightgray titlelabel.text = "challenges" titlelabel.textcolor = uicolor.red titlelabel.translatesautoresizingmaskintoconstraints = false collectionview.translatesautoresizingmaskintoconstraints = false self.view.addsubview(titlelabel) self.view.addsubview(collectionview) setupconstraints() } func setupconstraints(){ self.view.addconstraint(nslayoutconstraint(item: self.titlelabel, attribute: .height, relatedby: .equal, toitem: nil, attribute: .notanattribute, multiplier:0, constant:50.0 )) titlelabel.topanchor.constraint(equalto: self.view.topanchor).isactive = true titlelabel.leadinganchor.constraint(equalto: self.view.leadinganchor).isactive = true titlelabel.widthanchor.constraint(equalto: self.view.widthanchor).isactive = true collectionview.topanchor.constraint(equalto: titlelabel.bottomanchor).isactive = true collectionview.leadinganchor.constraint(equalto: self.view.leadinganchor).isactive = true collectionview.trailinganchor.constraint(equalto: self.view.trailinganchor).isactive = true collectionview.bottomanchor.constraint(equalto: self.view.bottomanchor).isactive = true } override func viewdidlayoutsubviews() { super.viewdidlayoutsubviews() let flowlayout = (collectionview.collectionviewlayout as! uicollectionviewflowlayout) flowlayout.itemsize = cgsize(width: collectionview.frame.width / 4.0 , height: collectionview.frame.width / 4.0) flowlayout.minimuminteritemspacing = self.collectionview.frame.width / 15.0 flowlayout.minimumlinespacing = self.collectionview.frame.width / 5.0 } } extension viewcontroller:uicollectionviewdatasource,uicollectionviewdelegate { func numberofsections(in collectionview: uicollectionview) -> int { return 1 } func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 10 } func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecell(withreuseidentifier: collectioncellidentifier, for: indexpath) cell.backgroundcolor = uicolor.gray return cell } }
Comments
Post a Comment