ios - Swift: Buttons not working inside UIScrollView -
i have scrollview. can scroll buttons don't allow press them(when press them don't anything). can fix this? here current code:
ps: i'm checking regularly if have question i'll answer.
class viewcontroller: uiviewcontroller { @iboutlet weak var categoryscrollview: uiscrollview! var categoryarr = ["button1","button2","button3","button4","button5", "button 6", "button 7", "button 8", "button 9", "button 10", "button 11", "button 12"] var buttoncolors = [uicolor.greencolor(), uicolor.bluecolor(), uicolor.blackcolor(), uicolor.cyancolor(), uicolor.magentacolor(), uicolor.greencolor(), uicolor.bluecolor(), uicolor.blackcolor(), uicolor.cyancolor(), uicolor.magentacolor(), uicolor.blackcolor(), uicolor.browncolor()] var buttonimages = [uiimage(named: "one"), uiimage(named: "two"), uiimage(named: "three"), uiimage(named: "playbuttonimage"), uiimage(named: "triangle"), uiimage(named: "playbuttonimage"), uiimage(named: "triangle"), uiimage(named: "playbuttonimage"), uiimage(named: "triangle"), uiimage(named: "playbuttonimage"), uiimage(named: "triangle"), uiimage(named: "playbuttonimage")] let kpadding:cgfloat = 20 override func viewdidload() { super.viewdidload() let buttonsize = cgsizemake(categoryscrollview.bounds.size.width/2, categoryscrollview.bounds.size.height/2)//hal let scrollingview = imagebuttonsview(buttonsize, buttoncount: 12) categoryscrollview.contentsize = scrollingview.frame.size categoryscrollview.addsubview(scrollingview) categoryscrollview.showsverticalscrollindicator = false categoryscrollview.delegate = self categoryscrollview.pagingenabled = true categoryscrollview.indicatorstyle = .default categoryscrollview.contentoffset = cgpointmake(0, 0) categoryscrollview.delayscontenttouches = false categoryscrollview.userinteractionenabled = true; categoryscrollview.exclusivetouch = true; categoryscrollview.cancancelcontenttouches = true; //categoryscrollview.clipstobounds = true } func imagebuttonsview(buttonsize:cgsize, buttoncount:int) -> uiview { let buttonview = uiview() buttonview.frame.origin = cgpointmake(50,142) let padding = cgsizemake(kpadding, kpadding) buttonview.frame.size.width = (buttonsize.width + padding.width) * cgfloat(buttoncount) var buttonposition = cgpointmake(0, padding.height) let buttonincrement = buttonsize.width + padding.width in 0...(buttoncount - 1) { let button = uibutton(type: .custom) buttonview.userinteractionenabled = true button.frame.size = buttonsize button.frame.origin = buttonposition buttonposition.x = buttonposition.x + buttonincrement button.settitle(categoryarr[i], forstate: uicontrolstate.normal) let buttonimagesone = buttonimages[i] button.setimage(buttonimagesone, forstate: .normal) button.layer.cornerradius = 30 button.addtarget(self, action: "pressed:", forcontrolevents: uicontrolevents.touchupinside) buttonview.addsubview(button) } //buttonview.backgroundcolor = uicolor.redcolor() categoryscrollview.bringsubviewtofront(buttonview) return buttonview } } extension viewcontroller:uiscrollviewdelegate{ func scrollviewdidenddecelerating(scrollview: uiscrollview) { let index = round(scrollview.contentoffset.x / scrollview.frame.size.width) print(index) } func pressed(/*sender: uibutton!*/) { let vc = uiviewcontroller(nibname: "gridviewcontroller", bundle: nil) self.presentviewcontroller(vc, animated: true, completion:nil) //viewcontroller.hidden = true } }
originally here, thought line
button.addtarget(self, action: "pressed:", forcontrolevents: uicontrolevents.touchupinside) should have said "pressed" without ":". turned out misunderstanding on part, sorry. should "pressed:".
instead, may related protocol extension , addtarget crashing nsinvalidargumentexception confusing.
Comments
Post a Comment