objective c - Set NSTableCellView and subviews to have dynamic width programmatically -
i have nstableview cells contain various nstextfields , nsprogressindicators. set cells programmatically , control behavior programmatically. way have implemented cells prevents me being able use interface builder. works great except when resize nswindow, nstablecells , subviews within nstableview not resize width equal width of table contained in.
any advice on how accomplish dynamic change in width nstableview's subviews appreciated.
here code creating cells:
func tableview(tableview: nstableview, viewfortablecolumn tablecolumn: nstablecolumn?, row: int) -> nsview? { var data = nsmutabledictionary() data = xloaddata(data) if data.count == 0 { data.setobject((homedir + "/library/messages/attachments"), forkey: "message attachments") data.setobject((homedir + "/library/caches"), forkey: "application caches") xsavedata(data) } let array: nsarray = data.allkeys.sorted(){($0 as! string) < ($1 as! string)} let mainview = nstablecellview(frame: nsrect(x: 0, y: 0, width: tableview.frame.width, height: 48)) mainview.identifier = "sx" let keyname = nstextfield(frame: nsrect(x: 0, y: 20, width: tableview.frame.width, height: 28)) keyname.bezeled = false keyname.drawsbackground = false keyname.editable = false keyname.selectable = false keyname.font = nsfont(name: "helveticaneue-thin", size: 17) keyname.textcolor = nscolor(hexcolorcode: "#f3f3f3") keyname.stringvalue = array.objectatindex(row) as! string //calculate sizes let val: string = valuefromkey(data, key: keyname.stringvalue) let size: double = sizedict.objectforkey(val) as! double let totalsize: double = sizedict.objectforkey("total") as! double var progratio: double = 0 if totalsize != 0 { progratio = (size / totalsize) * 100 } let sizeinfo = nstextfield(frame: nsrect(x: 0, y: 20, width: tableview.frame.width - 5, height: 28)) sizeinfo.bezeled = false sizeinfo.drawsbackground = false sizeinfo.editable = false sizeinfo.selectable = false sizeinfo.font = nsfont(name: "helveticaneue-thin", size: 16) sizeinfo.textcolor = nscolor(hexcolorcode: "#f3f3f3") sizeinfo.alignment = nstextalignment.righttextalignment sizeinfo.stringvalue = "\(convertbytes(size)) / \(totalsize)" var progasset = nsprogressindicator(frame: nsrect(x: 0, y: 0, width: tableview.frame.width - 5, height: 20)) progasset.indeterminate = false progasset.doublevalue = progratio mainview.addsubview(keyname) mainview.addsubview(sizeinfo) mainview.addsubview(progasset) return mainview }
you have 2 basic options: use auto layout or use old-style springs , struts (autoresizing masks).
for auto layout, create constraints relate views inside cell view cell view (their superview). might relate each other.
for example, have text fields overlapping, 1 right-aligned. should arrange them side-by-side, instead. like:
keyname.setcontenthuggingpriority(nslayoutpriority(250), fororientation:.horizontal) keyname.setcontentcompressionresistancepriority(nslayoutpriority(750), fororientation:.horizontal) sizeinfo.setcontenthuggingpriority(nslayoutpriority(251), fororientation:.horizontal) sizeinfo.setcontentcompressionresistancepriority(nslayoutpriority(751), fororientation:.horizontal) let views = ["keyname": keyname, "sizeinfo": sizeinfo] var constraints = nslayoutconstraint.constraintswithvisualformat("|[keyname][sizeinfo]-(5)-|", options:[.alignallbaseline], metrics:nil, views:views) nslayoutconstraint.activateconstraints(constraints)
etc. need horizontal constraints progress indicator , vertical constraints, too.
the other approach set autoresizingmask
each view control how each changes size , position superview changes.
Comments
Post a Comment