ios - Mixing storyboard and constraints in code -
i'm trying implement view controller wireframe
as have different screen resolutions - different number of yellow views can fit on screen. yellow views same size uiimageview
s, containing same image.
what i'm trying layout view controller in storyboard except yellow views. calculate number of yellow views screen can fit , add them using auto layout in code. this:
let numberofviewsperside = numberofviewsoneachside() let viewimage = uiimage (named: "repeat_image") var prevview = centerimageview if let viewimage = viewimage { index in 0...(numberofviewsperside - 1) { var newview = uiimageview(image: viewimage) newview.settranslatesautoresizingmaskintoconstraints(false) let views = ["prevview" : prevview, "newview" : newview] self.view.addsubview(newview) let constraint_h = nslayoutconstraint.constraintswithvisualformat("h:[newview]-15-[prevview]", options: nslayoutformatoptions.alignallcentery, metrics: nil, views: views) self.view.addconstraints(constraint_h) prevview = newview } }
the problem in viewdidload
, viewwillappear
methods auto layout isn't triggered yet. centerimageview
doesn't have right size , position. superview nil
@ point. can't add constraint view without parent. code works in viewdidappear
method means yellow views appear after transition animation completed.
do have idea how implement without doing in storyboard or purely in code. uilabel
, uibutton
on picture positioned deleted green centerview
- if add centerview
in code need set constraints screen in code.
update: solution offered @pteofil worked me call code above in viewwillappear
that:
override func viewwillappear(animated: bool) { super.viewwillappear(animated) self.view.layoutifneeded() addrepeatedimages() }
you can use layoutifneeded
autolayout triggered , have views correct size.
but yellow views looks use uicollectionview
manage them...
Comments
Post a Comment