ios - Why does an orientation change AutoLayout for my UITableViewCell? -
i trying use autolayout inside uitableviewcell objective-c. not using nib/xib/storyboard. create views in code.
here's uitableviewcell:
@implementation settlementtableviewcell - (instancetype) initwithreuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:uitableviewcellstyledefault reuseidentifier:reuseidentifier]; if (!self) return nil; [self setselectionstyle:uitableviewcellselectionstylenone]; _order = [[uilabel alloc] init]; [_order settranslatesautoresizingmaskintoconstraints:no]; [_order setfont:[uifont preferredfontfortextstyle:uifonttextstyleheadline]]; [[self contentview] addsubview:_order]; _amount = [[uilabel alloc] init]; [_amount settranslatesautoresizingmaskintoconstraints:no]; [_amount setfont:[uifont preferredfontfortextstyle:uifonttextstyleheadline]]; [_amount settextalignment:nstextalignmentright]; [[self contentview] addsubview:_amount]; _pickupdate = [[uilabel alloc] init]; [_pickupdate settranslatesautoresizingmaskintoconstraints:no]; [_pickupdate setfont:[uifont preferredfontfortextstyle:uifonttextstylesubheadline]]; [[self contentview] addsubview:_pickupdate]; _pickuplocation = [[uilabel alloc] init]; [_pickuplocation settranslatesautoresizingmaskintoconstraints:no]; [_pickuplocation setfont:[uifont preferredfontfortextstyle:uifonttextstylesubheadline]]; [_pickuplocation setadjustsfontsizetofitwidth:yes]; [_pickuplocation settextalignment:nstextalignmentright]; [[self contentview] addsubview:_pickuplocation]; _deliverydate = [[uilabel alloc] init]; [_deliverydate settranslatesautoresizingmaskintoconstraints:no]; [_deliverydate setfont:[uifont preferredfontfortextstyle:uifonttextstylesubheadline]]; [[self contentview] addsubview:_deliverydate]; _deliverylocation = [[uilabel alloc] init]; [_deliverylocation settranslatesautoresizingmaskintoconstraints:no]; [_deliverylocation setfont:[uifont preferredfontfortextstyle:uifonttextstylesubheadline]]; [_deliverylocation setadjustsfontsizetofitwidth:yes]; [_deliverylocation settextalignment:nstextalignmentright]; [[self contentview] addsubview:_deliverylocation]; [self setneedsupdateconstraints]; return self; } - (void) updateconstraints { [super updateconstraints]; uiedgeinsets padding = uiedgeinsetsmake(5, 20, -5, -20); [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_order attribute:nslayoutattributeleft relatedby:nslayoutrelationlessthanorequal toitem:[self contentview] attribute:nslayoutattributeleft multiplier:1 constant:padding.left]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_order attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributetop multiplier:1 constant:padding.top]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_amount attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributetop multiplier:1 constant:padding.top]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_amount attribute:nslayoutattributeright relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributeright multiplier:1 constant:padding.right]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_pickupdate attribute:nslayoutattributeleft relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributeleft multiplier:1 constant:padding.left]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_pickupdate attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:_order attribute:nslayoutattributebottom multiplier:1 constant:padding.top]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_pickuplocation attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:_pickupdate attribute:nslayoutattributetop multiplier:1 constant:0]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_pickuplocation attribute:nslayoutattributeleft relatedby:nslayoutrelationgreaterthanorequal toitem:_pickupdate attribute:nslayoutattributeright multiplier:1.0 constant:2]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_pickuplocation attribute:nslayoutattributeright relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributeright multiplier:1 constant:padding.right]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_deliverydate attribute:nslayoutattributeleft relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributeleft multiplier:1 constant:padding.left]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_deliverydate attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:_pickupdate attribute:nslayoutattributebottom multiplier:1 constant:0]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_deliverydate attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributebottom multiplier:1 constant:padding.bottom]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_deliverylocation attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:_deliverydate attribute:nslayoutattributetop multiplier:1 constant:0]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_deliverylocation attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributebottom multiplier:1 constant:padding.bottom]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_deliverylocation attribute:nslayoutattributeleft relatedby:nslayoutrelationgreaterthanorequal toitem:_deliverydate attribute:nslayoutattributeright multiplier:1.0 constant:2]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_deliverylocation attribute:nslayoutattributeright relatedby:nslayoutrelationequal toitem:[self contentview] attribute:nslayoutattributeright multiplier:1 constant:padding.right]]; // [_order setbackgroundcolor:[uicolor redcolor]]; // [_amount setbackgroundcolor:[uicolor orangecolor]]; // [_pickupdate setbackgroundcolor:[uicolor yellowcolor]]; // [_pickuplocation setbackgroundcolor:[uicolor greencolor]]; // [_deliverydate setbackgroundcolor:[uicolor bluecolor]]; // [_deliverylocation setbackgroundcolor:[uicolor purplecolor]]; } @end
here's uiviewcontroller:
@interface settlement : nsobject @property (nonatomic, strong) nsstring *orderid; @property (nonatomic, strong) nsstring *amount; @property (nonatomic, strong) nsstring *pickupdate; @property (nonatomic, strong) nsstring *deliverydate; @property (nonatomic, strong) nsstring *pickuplocation; @property (nonatomic, strong) nsstring *deliverylocation; @end @implementation settlement @end @interface settlementsviewcontroller () <uitableviewdelegate, uitableviewdatasource> @property (nonatomic, strong) nsmutablearray *rows; @property (nonatomic, strong) uitableview *tableview; @end @implementation settlementsviewcontroller - (instancetype) init { self = [super init]; if (!self) return nil; [self settitle:@"settlements"]; _rows = [[nsmutablearray alloc] initwithcapacity:5]; settlement *set = [[settlement alloc] init]; [set setorderid:@"01234567"]; [set setamount:@"$9999.99"]; [set setpickupdate:@"02/23/2015 0800"]; [set setdeliverydate:@"02/25/2015 1100"]; [set setpickuplocation:@"point field landing on severn, md"]; [set setdeliverylocation:@"winchester-on-the-severn, md"]; [_rows addobject:set]; set = [[settlement alloc] init]; [set setorderid:@"0006181"]; [set setamount:@"$3.42"]; [set setpickupdate:@"12/26/2013 1040"]; [set setdeliverydate:@"02/13/2014 0800"]; [set setpickuplocation:@"irondale, al"]; [set setdeliverylocation:@"lithonia, ga"]; [_rows addobject:set]; set = [[settlement alloc] init]; [set setorderid:@"0002586"]; [set setamount:@"$1.66"]; [set setpickupdate:@"09/27/2012 1350"]; [set setdeliverydate:@"02/09/2013 1115"]; [set setpickuplocation:@"decatur, al"]; [set setdeliverylocation:@"birmingham, al"]; [_rows addobject:set]; set = [[settlement alloc] init]; [set setorderid:@"0002586"]; [set setamount:@"$41.22"]; [set setpickupdate:@"11/08/2013 1608"]; [set setdeliverydate:@"11/11/2013 0000"]; [set setpickuplocation:@"birmingham, al"]; [set setdeliverylocation:@"simi valley, ca"]; [_rows addobject:set]; set = [[settlement alloc] init]; [set setorderid:@"0002586"]; [set setamount:@"$41.22"]; [set setpickupdate:@"11/08/2013 1608"]; [set setdeliverydate:@"11/11/2013 0000"]; [set setpickuplocation:@"birmingham, al"]; [set setdeliverylocation:@"simi valley, ca"]; [_rows addobject:set]; return self; } - (void) loadview { [super loadview]; _tableview = [[uitableview alloc] initwithframe:self.view.bounds style:uitableviewstylegrouped]; [_tableview setautoresizingmask:uiviewautoresizingflexibleheight | uiviewautoresizingflexiblewidth]; [_tableview setdelegate:self]; [_tableview setdatasource:self]; [_tableview setestimatedrowheight:72.0f]; [_tableview setrowheight:uitableviewautomaticdimension]; [[self view] addsubview:_tableview]; } - (nsinteger) tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return _rows.count; } - (uitableviewcell *) tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { settlementtableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell"]; if (!cell) cell = [[settlementtableviewcell alloc] initwithreuseidentifier:@"cell"]; settlement *settlement = [_rows objectatindex:indexpath.row]; [[cell order] settext:[settlement orderid]]; [[cell amount] settext:[settlement amount]]; [[cell pickupdate] settext:[settlement pickupdate]]; [[cell deliverydate] settext:[settlement deliverydate]]; [[cell pickuplocation] settext:[settlement pickuplocation]]; [[cell deliverylocation] settext:[settlement deliverylocation]]; return cell; } @end
when screen first loads, layout:
when change landscape, get:
when switch portrait, get:
this last state layout want. how can make initial layout 1 post-orientation change? missing?
this short of example make. can provide running project exhibits issue, don't know best way that. leave comment telling me how , i'll put out there.
thanks!
i continued play this. layout want if set width explicitly on both of date fields:
[[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_pickupdate attribute:nslayoutattributewidth relatedby:nslayoutrelationgreaterthanorequal toitem:nil attribute:nslayoutattributewidth multiplier:1.0 constant:120.0]]; [[self contentview] addconstraint:[nslayoutconstraint constraintwithitem:_deliverydate attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:_pickupdate attribute:nslayoutattributewidth multiplier:1 constant:0]];
can explain why intrinsic content size isn't enough? thanks!
Comments
Post a Comment