ios - Setting tableHeaderView height dynamically -


my application creates uitableviewcontroller contains custom tableheaderview may have arbitrary height. i've been struggling way set header dynamically, seems suggested ways have been cutting header short. uitableviewcontroller's relevant code:

import uikit import safariservices  class redditpostviewcontroller: uitableviewcontroller, networkcommunication, subviewlaunchlinkmanager {      //mark: uitableviewdatasource     var post: postdata?     var tree: commenttree?     weak var session: session! = session.sharedinstance      override func viewdidload() {         super.viewdidload()          // post info api         guard let postdata = post else { return }          //configure comment table         self.tableview.registerclass(redditpostcommenttableviewcell.self, forcellreuseidentifier: "commentcell")         let tableheader = postview(withpost: postdata, inviewcontroller: self)        let size = tableheader.systemlayoutsizefittingsize(uilayoutfittingexpandedsize)        let height = size.height        let width = size.width        tableheader.frame = cgrectmake(0, 0, width, height)        self.tableview.tableheaderview = tableheader          session.getredditpost(postdata) { (post) in            self.post = post?.post            self.tree = post?.comments            self.tableview.reloaddata()        }     } } 

this results in following incorrect layout: enter image description here if change line: tableheader.frame = cgrectmake(0, 0, width, height) tableheader.frame = cgrectmake(0, 0, width, 1000) tableheaderview lay out correctly: enter image description here

i'm not sure i'm doing incorrectly here. also, custom uiview class, if helps:

import uikit import foundation  protocol subviewlaunchlinkmanager: class {     func launchlink(sender: uibutton) }  class postview: uiview {      var body: uilabel?     var post: postdata?     var domain: uilabel?     var author: uilabel?     var selftext: uilabel?     var numcomments: uilabel?      required init?(coder adecoder: nscoder) {         fatalerror("not implemented yet")     }      init(withpost post: postdata, inviewcontroller viewcontroller: subviewlaunchlinkmanager) {         super.init(frame: cgrectzero)          self.post = post         self.backgroundcolor = uicolor.lightgraycolor()          let launchlink = uibutton()         launchlink.setimage(uiimage(named: "circle-user-7"), forstate: .normal)         launchlink.addtarget(viewcontroller, action: "launchlink:", forcontrolevents: .touchupinside)         self.addsubview(launchlink)          selftext = uilabel()         selftext?.backgroundcolor = uicolor.whitecolor()         selftext?.numberoflines = 0         selftext?.linebreakmode = .bywordwrapping         selftext!.text = post.selftext         self.addsubview(selftext!)         selftext?.sizetofit()          //let attributedstring = nsattributedstring(string: "test"/*post.selftexthtml*/, attributes: [nsdocumenttypedocumentattribute: nshtmltextdocumenttype])         //selftext.attributedtext = attributedstring          body = uilabel()         body!.text = post.title         body!.numberoflines = 0         body!.linebreakmode = .bywordwrapping         body!.textalignment = .justified         self.addsubview(body!)          domain = uilabel()         domain!.text = post.domain         self.addsubview(domain!)          author = uilabel()         author!.text = post.author         self.addsubview(author!)          numcomments = uilabel()         numcomments!.text = "\(post.numcomments)"         self.addsubview(numcomments!)          body!.translatesautoresizingmaskintoconstraints = false         domain!.translatesautoresizingmaskintoconstraints = false         author!.translatesautoresizingmaskintoconstraints = false         selftext!.translatesautoresizingmaskintoconstraints = false         launchlink.translatesautoresizingmaskintoconstraints = false         numcomments!.translatesautoresizingmaskintoconstraints = false          let views: [string: uiview] = ["body": body!, "domain": domain!, "author": author!, "numcomments": numcomments!, "launchlink": launchlink, "selftext": selftext!]         //let selftextsize = selftext?.sizethatfits((selftext?.frame.size)!)         //print(selftextsize)         //let metrics = ["selftextheight": selftextsize!.height]                     self.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-[body]-[selftext]-[domain]-|", options: [], metrics: nil, views: views))        self.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-[body]-[selftext]-[author]-|", options: [], metrics: nil, views: views))     self.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-[body]-[selftext]-[numcomments]-|", options: [], metrics: nil, views: views))     self.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-[launchlink]-[numcomments]-|", options: [], metrics: nil, views: views))     self.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|[body][launchlink]|", options: [], metrics: nil, views: views))     self.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|[selftext][launchlink]|", options: [], metrics: nil, views: views))     self.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|[domain][author][numcomments][launchlink]|", options: [], metrics: nil, views: views)) }  override func layoutsubviews() {     super.layoutsubviews()     body?.preferredmaxlayoutwidth = body!.bounds.width } } 

    //dynamically determine height of tableheaderview -> code result of effort. //http://collindonnell.com/2015/09/29/dynamically-sized-table-view-header-or-footer-using-auto-layout/ override func viewdidlayoutsubviews() {     super.viewdidlayoutsubviews()      if let headerview = tableview.tableheaderview {          let height = headerview.systemlayoutsizefitting(uilayoutfittingcompressedsize).height         var headerframe = headerview.frame          //comparison necessary avoid infinite loop         if height != headerframe.size.height {             headerframe.size.height = height             headerview.frame = headerframe             tableview.tableheaderview = headerview         }     } } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -