ios - How to use autolayout to prevent overlapping of arbitrary number of dynamically created UIViews -
this question exact duplicate of:
how can prevent multiple uiviews overlapping when number of uiviews isn't defined before hand? don't care placed on screen long padding definition met.
using autolayout seems way can't right.
i need have loop
uiview *prevview = nil; (uiview *aview in [self subviews]) { // need check foreach subview if view isn't overlapping // lets assume 2 views simplicity if (cgrectintersectsrect(aview.frame,prevview.frame)) { // update view frame // or update autolayout constraints } } // might need call function recursively if first pass wasn't successful
depending on specific wants , needs could:
position new subview correctly when creating it. if subviews don't move write logic make correct layout of subviews.
loop through subviews suggests , relocate each overlapping subview until it's no longer overlapping:
while(cgrectintersectsrect(aview.frame, prevview.frame)){ // re-locate aview [aview setcenter: cgpointmake(arc4random_uniform(self.view.frame.size.width), arc4random_uniform(self.view.frame.size.height))]; }
downside of course performance: lot of subviews take while (and many randomly generated numbers) find free space. if use should @ least make sure app won't stuck in infinite loop.
divide screen fixed size blocks , use these blocks position views. if subviews never bigger 50 * 50, example, divide screen x blocks of 50 * 50. keep track of these spaces array.
Comments
Post a Comment