ios - Wrong alignment of subviews in a UIScrollView -
i have following code, creates uiview
, , of subviews, , adds them uiscrollview
, in loop :
var count:cgfloat=bookscrollview.frame.minx var i=0;i<10;i++ { var view=uiview(frame: cgrect(x: count + 20, y: bookscrollview.frame.miny + 30, width: 200, height: 300)) view.backgroundcolor=uicolor.whitecolor() view.layer.cornerradius = 5; view.layer.maskstobounds = true; var imageview=uiimageview(frame: cgrect(x: count, y: view.frame.miny - 30, width: 150, height: 220)) // imageview.image=uiimage(named: "sample_book")! view.addsubview(imageview) var titlelabel=uilabel(frame: cgrect(x: count + 10, y: imageview.frame.maxy + 30, width: 185, height: 60)) titlelabel.text="head first javascript" titlelabel.backgroundcolor=uicolor.clearcolor() titlelabel.font=uifont(name: "menlo-bold ", size: 15) titlelabel.textalignment = nstextalignment.center titlelabel.textcolor=uicolor.graycolor() view.addsubview(titlelabel) bookscrollview.addsubview(view) count+=220 } bookscrollview.contentsize=cgsize(width: count, height: 200)
it works fine,except fact other in first view
,imageview
, titlelabel
not visible.
the label , imageview have moved towards right second view onwards.
frames expressed according superview's coordinate space.
since you're adding image view , label view
not scroll view, frames should specified in view
's coordinate space. not need add count
x position.
var imageview=uiimageview(frame: cgrect(x: 0.0, y: 0.0, width: 150, height: 220))
and:
var titlelabel=uilabel(frame: cgrect(x: 10.0, y: imageview.frame.maxy + 30, width: 185, height: 60))
note: should uicollectionview
, autolayout more robust way of achieving this.
Comments
Post a Comment