ios - uiscrollview's autolayout,The view hierarchy is not prepared for the constraint -
i want add scrolling imageviews scrollviews using autolayout.
the view hierarchy folllows:
---|
|---placeholder view
|---scroll view
the code follows:
-(uicollectionviewcell*)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {
static nsstring* identifier = @"wcollectionviewcell"; wcollectionviewcell* cell =[collectionview dequeuereusablecellwithreuseidentifier:identifier forindexpath:indexpath]; cell.imagescrollview.backgroundcolor=[uicolor orangecolor]; uiimageview* imageview1 = [[uiimageview alloc]init]; imageview1.image =[uiimage imagenamed:@"1.jpg"]; imageview1.translatesautoresizingmaskintoconstraints=no; [cell.imagescrollview addsubview:imageview1]; nsdictionary* views = @{@"imageview1":imageview1,@"placeview":cell.placeholderview,@"scrollview":cell.imagescrollview}; //set imageview'size nsarray *img_constraint_h = [nslayoutconstraint constraintswithvisualformat:@"v:[imageview1(placeview)]" options:0 metrics:nil views:views]; nsarray *img_constraint_v = [nslayoutconstraint constraintswithvisualformat:@"h:[imageview1(placeview)]" options:0 metrics:nil views:views]; [self.view addconstraints:img_constraint_h]; [self.view addconstraints:img_constraint_v]; //set imageview's position nsarray* top_position = [nslayoutconstraint constraintswithvisualformat:@"h:|-0-[imageview1]" options:0 metrics:nil views:views]; nsarray* bottom_position = [nslayoutconstraint constraintswithvisualformat:@"v:|-0-[imageview1]" options:0 metrics:nil views:views]; [cell.imagescrollview addconstraints:top_position]; [cell.imagescrollview addconstraints:bottom_position]; return cell;
}
--------error---------- 2015-04-10 12:20:46.245 t[92361:1319616] view hierarchy not prepared constraint: when added view, constraint's items must descendants of view (or view itself). crash if constraint needs resolved before view hierarchy assembled. break on -[uiview _viewhierarchyunpreparedforconstraint:] debug. 2015-04-10 12:20:46.246 t[92361:1319616] * assertion failure in -[uiview _layoutengine_didaddlayoutconstraint:roundingadjustment:mutuallyexclusiveconstraints:], /sourcecache/uikit_sim/uikit-3318.93/nslayoutconstraint_uikitadditions.m:560 2015-04-10 12:20:46.249 t[92361:1319616] * terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'impossible set layout view hierarchy unprepared constraint.'
your problem adding constraints height , width of image self.view
not parent view of image.
you need add constraints cell.imagescrollview
, cell superview or image itself.
so both of should work:
//set imageview'size nsarray *img_constraint_h = [nslayoutconstraint constraintswithvisualformat:@"v:[imageview1(placeview)]" options:0 metrics:nil views:views]; nsarray *img_constraint_v = [nslayoutconstraint constraintswithvisualformat:@"h:[imageview1(placeview)]" options:0 metrics:nil views:views]; [cell.imagescrollview addconstraints:img_constraint_h]; [cell.imagescrollview addconstraints:img_constraint_v];
or
[imageview1 addconstraints:img_constraint_h]; [imageview1 addconstraints:img_constraint_v];
you have both options because constraints don't reference parent, imageview. if there |
in visual format, first option work.
one more tip:
you add constraints @ once:
wcollectionviewcell* cell =[collectionview dequeuereusablecellwithreuseidentifier:identifier forindexpath:indexpath]; cell.imagescrollview.backgroundcolor=[uicolor orangecolor]; uiimageview* imageview1 = [[uiimageview alloc]init]; imageview1.image =[uiimage imagenamed:@"1.jpg"]; imageview1.translatesautoresizingmaskintoconstraints=no; [cell.imagescrollview addsubview:imageview1]; nsdictionary* views = @{@"imageview1":imageview1,@"placeview":cell.placeholderview,@"scrollview":cell.imagescrollview}; //set imageview's position nsarray* horizontal_constraints = [nslayoutconstraint constraintswithvisualformat:@"h:|-0-[imageview1(placeview)]" options:0 metrics:nil views:views]; nsarray* vertical_constraints = [nslayoutconstraint constraintswithvisualformat:@"v:|-0-[imageview1(placeview)]" options:0 metrics:nil views:views]; [cell.imagescrollview addconstraints:horizontal_constraints]; [cell.imagescrollview addconstraints:vertical_constraints]; return cell;
let me know how goes, or if need more help!