ios - Hot to add a View to a Scrollview programmatically using layout constraints -
i'm triying instert programatically view inside uiscrollview, doesn't appear here code:
mainscrollview.translatesautoresizingmaskintoconstraints = false self.view.addsubview(mainscrollview) //add trailing let trailingconstraint = nslayoutconstraint(item: mainscrollview, attribute: .trailing, relatedby: .equal, toitem: self.view, attribute: .trailing, multiplier: 1, constant: 0) self.view.addconstraint(trailingconstraint) //add leading let leadingconstraint = nslayoutconstraint(item: mainscrollview, attribute: .leading, relatedby: .equal, toitem: self.view, attribute: .leading, multiplier: 1, constant: 0) self.view.addconstraint(leadingconstraint) //add top let topconstraint = nslayoutconstraint(item: mainscrollview, attribute: .top, relatedby: .equal, toitem: self.view, attribute: .top, multiplier: 1, constant: 0) self.view.addconstraint(topconstraint) //add bottom let bottomconstraint = nslayoutconstraint(item: mainscrollview, attribute: .bottom, relatedby: .equal, toitem: self.view, attribute: .bottom, multiplier: 1, constant: 0) self.view.addconstraint(bottomconstraint) let contentview = uiview() contentview.translatesautoresizingmaskintoconstraints = false contentview.backgroundcolor = .blue mainscrollview.addsubview(contentview) //add trailing let trailingconstraintcontent = nslayoutconstraint(item: contentview, attribute: .trailing, relatedby: .equal, toitem: mainscrollview, attribute: .trailing, multiplier: 1, constant: 0) mainscrollview.addconstraint(trailingconstraintcontent) //add leading let leadingconstraintcontent = nslayoutconstraint(item: contentview, attribute: .leading, relatedby: .equal, toitem: mainscrollview, attribute: .leading, multiplier: 1, constant: 0) mainscrollview.addconstraint(leadingconstraintcontent) //add top let topconstraintcontent = nslayoutconstraint(item: contentview, attribute: .top, relatedby: .equal, toitem: mainscrollview, attribute: .top, multiplier: 1, constant: 0) mainscrollview.addconstraint(topconstraintcontent) //add bottom let bottomconstraintcontent = nslayoutconstraint(item: contentview, attribute: .bottom, relatedby: .equal, toitem: mainscrollview, attribute: .bottom, multiplier: 1, constant: 0) mainscrollview.addconstraint(bottomconstraintcontent)
the first scrollview inserted because added background color , can see it, cant see contentview added background color blue.
any help?
update
i have tried following , no success:
mainscrollview.translatesautoresizingmaskintoconstraints = false let contentview = uiview() contentview.translatesautoresizingmaskintoconstraints = false mainscrollview.backgroundcolor = .red contentview.backgroundcolor = .blue self.view.addsubview(mainscrollview) mainscrollview.addsubview(contentview) let viewsdictionary = ["mainscrollview": mainscrollview, "contentview": contentview] let mainscrollviewverticalconstraint = nslayoutconstraint.constraints(withvisualformat: "v:|[mainscrollview]|", options: [], metrics: nil, views: viewsdictionary) let mainscrollviewhorizontalconstraint = nslayoutconstraint.constraints(withvisualformat: "h:|[mainscrollview]|", options: [], metrics: nil, views: viewsdictionary) self.view.addconstraints(mainscrollviewverticalconstraint) self.view.addconstraints(mainscrollviewhorizontalconstraint) let contentviewverticalconstraint = nslayoutconstraint.constraints(withvisualformat: "v:|[contentview]|", options: [], metrics: nil, views: viewsdictionary) let contentviewhorizontalconstraint = nslayoutconstraint.constraints(withvisualformat: "h:|[contentview]|", options: [], metrics: nil, views: viewsdictionary) mainscrollview.addconstraints(contentviewverticalconstraint) mainscrollview.addconstraints(contentviewhorizontalconstraint)
when adding sub views important thing remember pin them in way make possible auto layout evaluate height, because scroll view determines content size determining sum of heights sub views (in case of vertical). should specify height in vfl . eg.
let contentviewverticalconstraint=nslayoutconstraint.constraints(withvisualformat: "v:|[contentview(300)]", options: [], metrics: nil, views: viewsdictionary)
Comments
Post a Comment