ios - Setting images of a button -
i have come across issue cannot resolve.
i have struct , change image of button in each collection view. text/image works according plan. however, when trying change image button struggling. please me?
class collectionviewcontent{ var caption = "" var mainimage: uiimage! var userprofileimage : uibutton init(caption: string, mainimage: uiimage!, userprofileimage : uibutton! ) { self.description = description self.mainimage = featuredimage self.userprofileimage = posteduserprofileimage } static func showposts() -> [posts] { return [ posts(description: "sweet", mainimage: uiimage(named: "image 1"), posteduserprofileimage:!), posts(description: "awesome", mainimage: uiimage(named: "image 2")!), posts(description: "wow!", mainimage: uiimage(named: "image 3")! ) ] } } this collectionviewcell class
class colviewcontetcollectionviewcell: uicollectionviewcell { var posts: posts! { didset { updateui() } } @iboutlet weak var featuredimage: uiimageview! @iboutlet weak var postcaptionlabel: uilabel! @iboutlet weak var posteduserprofileimage: uibutton! func updateui() { postcaptionlabel?.text! = posts.title featuredimage?.image! = posts.featuredimage posteduserprofileimage = posts.posteduserprofileimage } override func layoutsubviews() { super.layoutsubviews() self.layer.cornerradius = 10.0 self.clipstobounds = true } } i think line of code incorrect way, not sure.
posteduserprofileimage = posts.posteduserprofileimage
instead of:
posteduserprofileimage = posts.posteduserprofileimage you should do:
posteduserprofileimage.setimage(posts.posteduserprofileimage.image(for: uicontrolstate.normal), for: uicontrolstate.normal) since posteduserprofileimage uibutton, need use setimage method set it's image control specific control state. if no images set other control states, image normal state used others too.
and since posts.posteduserprofileimage uibutton (as mentioned in comments below) need button's image via image(for:) method.
Comments
Post a Comment