ios - How to use a custom UIView multiple times in a controller with different content? -
i have created own custom uiview popover have display on screen. uiview looks this:
class helptipspopover: uiview { weak var title: uilabel! weak var mytext: uilabel! override init(frame: cgrect) { super.init(frame: frame) let strongtitle = uilabel() title = strongtitle let strongmytext = uilabel() mytext = strongmytext self.addsubview(strongtitle) title.translatesautoresizingmaskintoconstraints = false if selected == true{ title.text = "search" title.font = uifont(name: "helveticaneue-bold", size: 12) title.textcolor = uicolor.trlmblueblackcolor() let leftconstraint = nslayoutconstraint(item: title, attribute: .leading, relatedby: .equal, toitem: self, attribute: .leading, multiplier: 1.0, constant: 10) let topconstraint = nslayoutconstraint(item: title, attribute: .top, relatedby: .equal, toitem: self, attribute: .top, multiplier: 1.0, constant: 10) self.addconstraints([leftconstraint, topconstraint]) self.addsubview(strongmytext) mytext.translatesautoresizingmaskintoconstraints = false mytext.text = "search equities view order book , market prints specific moments in time." mytext.numberoflines = 0 mytext.linebreakmode = nslinebreakmode.bywordwrapping mytext.font = uifont(name: "helvetica neue", size: 12) mytext.textcolor = uicolor.trlmblueblackcolor() let leftdescription = nslayoutconstraint(item: mytext, attribute: .leading, relatedby: .equal, toitem: self, attribute: .leading, multiplier: 1.0, constant: 10) let rightdescription = nslayoutconstraint(item: mytext, attribute: .trailing, relatedby: .equal, toitem: self, attribute: .trailing, multiplier: 1.0, constant: 10) let topdescription = nslayoutconstraint(item: mytext, attribute: .top, relatedby: .equal, toitem: title, attribute: .bottom, multiplier: 1.0, constant: 5) self.addconstraints([leftdescription, topdescription, rightdescription]) } required init(coder adecoder: nscoder) { super.init(coder: adecoder)! } }
now have 3 popovers have display in view controller different title , text in each popover. here methods in view controller displays popovers:
func showpopover(){ self.view.addsubview(helptipspopover) helptipspopover.tag = 1 helptipspopover.translatesautoresizingmaskintoconstraints = false helptipspopover.layer.cornerradius = 6 helptipspopover.backgroundcolor = uicolor(white: 1.0, alpha: 0.8) let leftconstraint = nslayoutconstraint(item: helptipspopover, attribute: .leading, relatedby: .equal, toitem: self.view, attribute: .leading, multiplier: 1.0, constant: 10) let topconstraint = nslayoutconstraint(item: helptipspopover, attribute: .top, relatedby: .equal, toitem: self.hotspotone, attribute: .bottom, multiplier: 1.0, constant: 4) let widthconstraint = nslayoutconstraint(item: helptipspopover, attribute: nslayoutattribute.width, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1.0, constant: 200) let heightconstraint = nslayoutconstraint(item: self.helptipspopover, attribute: nslayoutattribute.height, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1.0, constant: 80) self.view.addconstraints([leftconstraint, topconstraint, widthconstraint, heightconstraint]) } func showpopovertwo(){ self.view.addsubview(helptipspopover) helptipspopover.tag = 1 helptipspopover.translatesautoresizingmaskintoconstraints = false helptipspopover.layer.cornerradius = 6 helptipspopover.backgroundcolor = uicolor(white: 1.0, alpha: 0.8) let centerconstraint = nslayoutconstraint(item: helptipspopover, attribute: .centerx, relatedby: .equal, toitem: self.view, attribute: .centerx, multiplier: 1.0, constant: 0) let topconstraint = nslayoutconstraint(item: helptipspopover, attribute: .top, relatedby: .equal, toitem: self.hotspottwo, attribute: .bottom, multiplier: 1.0, constant: 4) let widthconstraint = nslayoutconstraint(item: helptipspopover, attribute: nslayoutattribute.width, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1.0, constant: 200) let heightconstraint = nslayoutconstraint(item: self.helptipspopover, attribute: nslayoutattribute.height, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1.0, constant: 80) self.view.addconstraints([centerconstraint, topconstraint, widthconstraint, heightconstraint]) }
now want achieve this:
so each popover should have different title , text want reuse same uiview. appreciated. thanks!
modify init method
init(frame: cgrect, titlestring: string, bodystring: string) { super.init(frame: frame) // current initializer title.text = titlestring mytext.text = bodystring }
then can initialize popup this:
let frame = cgrectmake(0,0,180,100) let titlestring = "my custom title" let bodystring = "this body. i'm explaining things here." helptipspopoverone = helptipspopover(frame: frame, titlestring: titlestring, bodystring: bodystring)
modify display method take popup parameter. can create popups , display whatever text you'd like!
Comments
Post a Comment