ios - UIButton action not called when added as a subview in UIView with setTranslatesAutoresizingMaskIntoConstraints(false) -
ios - UIButton action not called when added as a subview in UIView with setTranslatesAutoresizingMaskIntoConstraints(false) -
uibutton "pressed" func not called when added subview of uiview. code below. work when removed settranslatesautoresizingmaskintoconstraints(false). need utilize method auto layout resizing.
var myview = uiview() allow orderbook = uibutton() override func viewdidload() { super.viewdidload() myview.backgroundcolor = uicolor.redcolor() myview.settranslatesautoresizingmaskintoconstraints(false) self.view.addsubview(myview) allow views1 = ["myview" : myview] var constv = nslayoutconstraint.constraintswithvisualformat("v:|-[myview(>=100)]-|", options: nslayoutformatoptions.directionleadingtotrailing, metrics: nil, views: views1) var consth = nslayoutconstraint.constraintswithvisualformat("h:[myview(==100)]|", options: nslayoutformatoptions.directionleadingtotrailing, metrics: nil, views: views1) self.view.addconstraints(consth) self.view.addconstraints(constv) orderbook.settitle("order book", forstate: uicontrolstate.normal) orderbook.settitlecolor(uicolor.bluecolor(), forstate: uicontrolstate.normal) orderbook.addtarget(self, action: "pressed:", forcontrolevents: uicontrolevents.touchupinside) myview.addsubview(orderbook) } func pressed(sender: uibutton!) { println("pressed") } override func viewdidlayoutsubviews() { orderbook.frame = cgrectmake(0, 0, myview.frame.width, 100) }
please assist. in advances.
i understand response bit late, i'll respond anyway still having issue, or have come workaround.
the reason why unable interact uibutton after have added uiview programmatically because have not added of necessary constraints uibutton. in particular case didn't add together width/height constraint. below illustration of button created programmatically added uiview created programatically. notice constraints added button.
var popupview = uiview() popupview.settranslatesautoresizingmaskintoconstraints(false) popupview.backgroundcolor = color.fromhex("#ffffff", alpha: 1) popupview.layer.cornerradius = 20 popupview.clipstobounds = true var bottomview = uiview(frame: cgrectmake(0, 0, popupview.frame.size.width, 80)) bottomview.settranslatesautoresizingmaskintoconstraints(false) var singlebtn = uibutton() singlebtn.titlelabel?.font = uifont(name: "nunito-regular", size: 20) singlebtn.settitlecolor(color.fromhex("#979797", alpha: 1), forstate: uicontrolstate.normal) singlebtn.settitlecolor(color.fromhex("#56daf0", alpha: 1), forstate: uicontrolstate.highlighted) singlebtn.settitle("ok", forstate: uicontrolstate.normal) singlebtn.addtarget(self, action: "singlebtnaction:", forcontrolevents: uicontrolevents.touchupinside) singlebtn.settranslatesautoresizingmaskintoconstraints(false) singlebtn.backgroundcolor = uicolor.redcolor() bottomview.addsubview(singlebtn) bottomview.addconstraint(nslayoutconstraint(item: singlebtn, attribute: nslayoutattribute.width, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1.0, constant: 50)) bottomview.addconstraint(nslayoutconstraint(item: singlebtn, attribute: nslayoutattribute.height, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1.0, constant: 50)) bottomview.addconstraint(nslayoutconstraint(item: singlebtn, attribute: nslayoutattribute.centerx, relatedby: nslayoutrelation.equal, toitem: bottomview, attribute: nslayoutattribute.centerx, multiplier: 1.0, constant: 0)) bottomview.addconstraint(nslayoutconstraint(item: singlebtn, attribute: nslayoutattribute.centery, relatedby: nslayoutrelation.equal, toitem: bottomview, attribute: nslayoutattribute.centery, multiplier: 1.0, constant: 0))
ios swift uiview uibutton
Comments
Post a Comment