ios - adding a constraint to a subview makes background color not display -


so using custom function format subview adding uicollectionviewcell. brian voong's public project here: https://github.com/purelyswift/facebook_feed_dynamic_cell_content/blob/master/facebookfeed2/viewcontroller.swift.

func addconstraintswithformat(format: string, views: uiview...) {    var viewsdictionary = [string: uiview]()    (index, view) in views.enumerate() {       let key = "v\(index)"       viewsdictionary[key] = view       view.translatesautoresizingmaskintoconstraints = false    }    addconstraints(nslayoutconstraint.constraintswithvisualformat(format, options: nslayoutformatoptions(), metrics: nil, views: viewsdictionary)) } 

what interesting, in uicollectionview add subview single cell, , set background color white. background white when comment out line sets background subview, , no background color set when uncomment out line setting visually formatted constraints subview.

here 2 lines clobber each other:

func chronicleoneclicked(sender: uibutton) {    point1view.backgroundcolor = uicolor.whitecolor()     addsubview(point1view)    //when below commented background of point1view disappears       //addconstraintswithformat("|-50-[v0]-50-|", views: point1view) } 

when print(subviews) see uiview white background color highest in view stack (top of stack). when print out subviews[subviews.count-1].backgroundcolor optional(uidevicewhitecolorspace 1 1) expect. strange because color not displayed.

i not sure how go seeing happening behind scenes confirm background being set @ in latter case.

this happens in class uicollectionviewcell using class of 1 of uicollectionview cells can viewed in entirety here:

https://gist.github.com/ebbnormal/edb79a15dab4797946e0d1f6905c2dd0

here screen shot both cases, first case line addconstraintswithformat commented out, , second case uncommented: subview of point1subview highlighted white background in first case.

enter image description here

enter image description here

this how setup views. happens in class overrides uicollectionviewcell

class myclass : uicollectionviewcell {    var chronicle: browsablechronicle? {       didset{          //etc.          point1.addtarget(self, action: #selector(chronicleoneclicked(_:)), forcontrolevents: uicontrolevents.touchupinside)       }    }     override init(frame: cgrect) {       super.init(frame: frame)       setupviews()    }     let point1 : pointbuttonview = {       let pointview = pointbuttonview(frame: cgrectmake(0, 0, 25, 25 ))       return pointview    }()    //note here create view, background doesn't display    let point1view : uiview = {       let pointview = uiview(frame: cgrectmake( 0, 0, 200,  270))       pointview.backgroundcolor = uicolor.whitecolor()       let title = uilabel(frame: cgrectmake(0, 0, 200, 21))       title.font = uifont(name:"helveticaneue-bold", size: 16.0)        pointview.addsubview(title)       let summary = uilabel(frame: cgrectmake(0, 0, 190, 260))       summary.linebreakmode = nslinebreakmode.bywordwrapping       summary.numberoflines = 4       summary.font = uifont(name:"helveticaneue", size: 12.5)       pointview.addsubview(summary)        let button = uibutton(frame: cgrectmake(0, 200, 190, 30))       button.backgroundcolor = uicolor(red:0.00, green:0.90, blue:0.93, alpha:1.0)       pointview.addsubview(button)        pointview.tag = 100        return pointview    }()     //note: here add subview uicollectionviewcell view    func chronicleoneclicked(sender: uibutton){       addsubview(point1view)       addconstraintswithformat("h:|-20-[v0]-20-|", views: point1view)       //todo anytime add constraint here background color leaves!       print(subviews[subviews.count-1].backgroundcolor) //prints white    } } 

update: thought maybe related issue :

uitableviewcell subview disappears when cell selected

where uicollectionviewcell selected, , therefore ios automatically sets backgroundcolor clear. problem is, implemented class extension of uiview see when didset called on backgroundcolor , when set clear, set white. however, calls didset on backgroundcolor once, when first set color of view. here code used override uiview class:

class neverclearview: uiview {    override var backgroundcolor: uicolor? {       didset {          print("background color being set")          if backgroundcolor == uicolor.clearcolor() {             print("set clear color")             backgroundcolor = uicolor.whitecolor()          }       }    } } 

the difference seeing caused view frame resulting in 0 width or 0 height.

let's explain how drawing system works.

every view has layer draws background color in bounds, specified view frame. every subview drawn. however, subviews not limited frame unless set uiview.clipstobounds true.

what seeing means container view has 0 frame (either width or height) subviews have correct frame, therefore displayed correctly.

there multiple reasons why happen, example:

  1. you setting translatesautoresizingmaskintoconstraints false system view (e.g. content view of uicollectionview).
  2. you have constraint conflict, resulting in important constraint removed (you should see warning).
  3. you missing constraints. specifically, don't see setting vertical constraints.

you should able debug problem using view debugger in xcode. open app, click view debugger button , print recursive description of cell. should see frame zero.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -