ios - How to create custom uiview that button can be clicked inside uiview? -
i tried create clicked scrollview button (at bottom):
this code far:
scview = uiscrollview(frame: cgrect(x: 0, y: view.frame.maxy-110, width: view.bounds.width, height: 110)) self.view.addsubview(scview) scview.backgroundcolor = uicolor.lightgray scview.translatesautoresizingmaskintoconstraints = false in 0 ... 10 { let mynewview=uiview(frame: cgrect(x: xoffset, y: cgfloat(buttonpadding), width: 200, height: 100)) mynewview.backgroundcolor=uicolor.white mynewview.layer.cornerradius=10 self.scview.addsubview(mynewview) let label = uilabel(frame: cgrect(x: xoffset, y: cgfloat(buttonpadding)+5, width: 150, height: 10)) label.center = cgpoint(x: 160, y: 285) label.textalignment = .left label.text = "i'am title label" label.textcolor = .black mynewview.addsubview(label) let ditancelabel = uilabel(frame: cgrect(x: xoffset, y: cgfloat(buttonpadding)+5, width: 50, height: 10)) ditancelabel.center = cgpoint(x: 160, y: 285) ditancelabel.textalignment = .right ditancelabel.text = "0.04 km" ditancelabel.textcolor = .red mynewview.addsubview(ditancelabel) let textview = uitextview(frame: cgrect(x: xoffset, y: cgfloat(buttonpadding)+15, width: 200.0, height: 40.0)) self.automaticallyadjustsscrollviewinsets = false textview.center = self.view.center textview.textalignment = nstextalignment.justified textview.textcolor = uicolor.lightgray textview.backgroundcolor = uicolor.clear mynewview.addsubview(textview) let button = uibutton() button.tag = button.backgroundcolor = uicolor.white button.settitle("\(i)", for: .normal) button.settitlecolor(.black, for: .normal) button.backgroundcolor = uicolor.clear button.addtarget(self, action:#selector(handleregister(sender:)), for: .touchupinside) button.frame = cgrect(x: xoffset, y: cgfloat(buttonpadding), width: 200, height: 100) xoffset = xoffset + cgfloat(buttonpadding) + button.frame.size.width mynewview.addsubview(button) } scview.contentsize = cgsize(width: xoffset, height: scview.frame.height)
the first button can clicked, other cannot clicked. , 2 uilabels , 1 uitextview did not appear.
the handleregister
method sender tag , print log know button can clicked or not.
how repair code can custom uiview in above image?
i need transfer label , textview text button click viewcontroller handle seque in handleregister
.
1) can't see labels , other control because of container view size (200, 100)
let mynewview=uiview(frame: cgrect(x: xoffset, y: cgfloat(buttonpadding), width: 200, height: 100))
and set center of controls.
label.center = cgpoint(x: 160, y: 285) ditancelabel.center = cgpoint(x: 160, y: 285)
this center point not visible within container view. that's why can't see labels within view. just comment these line set center of labels , can see perfectly.
2) need transfer label , textview text button click viewcontroller handle seque in handleregister.
i hope have array in stored data display in custom view, find object array based on button tag, , pass object control.
3) first button can clicked, other cannot clicked.
again problem same labels. had set frame of button this
button.frame = cgrect(x: xoffset, y: cgfloat(buttonpadding), width: 200, height: 100)
and increase xoffset line create view
xoffset = xoffset + cgfloat(buttonpadding) + button.frame.size.width
xoffset increased each time size of button. if print xoffset value print (assuming initial value of xoffset = 10, padding = 0)
for in 0 ... 10 { print(xoffset) ..... other code }
output: 10, 210, 410, 610...... because of button out of bound container view , because of can't click on that.
i have updated code, check below
scview = uiscrollview(frame: cgrect(x: 0, y: view.frame.maxy-110, width: view.bounds.width, height: 110)) self.view.addsubview(scview) scview.backgroundcolor = uicolor.lightgray scview.translatesautoresizingmaskintoconstraints = false in 0 ... 10 { let mynewview=uiview(frame: cgrect(x: xoffset, y: cgfloat(buttonpadding), width: 200, height: 100)) mynewview.backgroundcolor=uicolor.white mynewview.layer.cornerradius=10 self.scview.addsubview(mynewview) let subvwoffset = 5 let label = uilabel(frame: cgrect(x: subvwoffset, y: cgfloat(buttonpadding)+5, width: 150, height: 10)) label.textalignment = .left label.text = "i'am title label" label.textcolor = .black mynewview.addsubview(label) let ditancelabel = uilabel(frame: cgrect(x: subvwoffset, y: cgfloat(buttonpadding)+5, width: 50, height: 10)) ditancelabel.textalignment = .right ditancelabel.text = "0.04 km" ditancelabel.textcolor = .red mynewview.addsubview(ditancelabel) let textview = uitextview(frame: cgrect(x: subvwoffset, y: cgfloat(buttonpadding)+15, width: 200.0, height: 40.0)) self.automaticallyadjustsscrollviewinsets = false textview.center = self.view.center textview.textalignment = nstextalignment.justified textview.textcolor = uicolor.lightgray textview.backgroundcolor = uicolor.clear mynewview.addsubview(textview) let button = uibutton() button.tag = button.backgroundcolor = uicolor.white button.settitle("\(i)", for: .normal) button.settitlecolor(.black, for: .normal) button.backgroundcolor = uicolor.clear button.addtarget(self, action:#selector(handleregister(sender:)), for: .touchupinside) button.frame = cgrect(x: subvwoffset, y: cgfloat(buttonpadding), width: 200, height: 100) xoffset = xoffset + cgfloat(buttonpadding) + button.frame.size.width mynewview.addsubview(button) } scview.contentsize = cgsize(width: xoffset, height: scview.frame.height)
let me know if find other difficulty.
Comments
Post a Comment