ios - iOS8 incorrrect value of UIView.systemLayoutSizeFittingSize with Size Classes -
i've faced strange issue method uiview.systemlayoutsizefittingsize
when size classes
being on on ios 8. example i'm usign paul's tutorial project auto height cell. project mine modifications demonstrate purpose, can found here
so on ios 7 looks nice.
but on ios 8 method return more lesser value , cells starting breaking apart.
this issue gone when size classes
being off. don't know how aproach , @ point solution know turn of size classes
.
code
viewcontroller.m (108 lines)
#import "viewcontroller.h" #import "customtableviewcell.h" @interface viewcontroller () <uitableviewdatasource, uitableviewdelegate> { nsmutablearray *_fontarray; nsmutablearray *_quotearray; } @property (weak, nonatomic) iboutlet uitableview *tableview; @property (strong, nonatomic) customtableviewcell *customcell; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. self.tableview.delegate = self; self.tableview.datasource = self; // self.navigationitem.rightbarbuttonitem = self.editbuttonitem; _fontarray = [[uifont familynames] mutablecopy]; for(int = 0; < 100; i++) { [_fontarray addobjectsfromarray:[uifont familynames]]; } nslog(@"size: %d", [_fontarray count]); _quotearray = [@[@"for past 33 years, have looked in mirror every morning , asked myself: 'if today last day of life, want today?' , whenever answer has been 'no' many days in row, know need change something. -steve jobs", @"be yardstick of quality. people aren't used environment excellence expected. - steve jobs", @"innovation distinguishes between leader , follower. -steve jobs"] mutablecopy]; // use ios 8 new auto sizing feature heights (don't need calculate yourself) // _tableview.rowheight = uitableviewautomaticdimension; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [_fontarray count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { customtableviewcell *cell = [self.tableview dequeuereusablecellwithidentifier:@"customcell"]; cell.fontnamelabel.text = _fontarray[indexpath.row]; int quoteindex = indexpath.row % [_quotearray count]; cell.quotelabel.text = _quotearray[quoteindex]; cell.quotelabel2.text = _quotearray[quoteindex]; nsstring *fontname = _fontarray[indexpath.row]; cell.quotelabel.font = [uifont fontwithname:fontname size:17]; cell.quotelabel2.font = [uifont fontwithname:fontname size:17]; return cell; } // note: in ios 8 can use automatic height calculations autolayout, // , can avoid writing height method. comment out, , uncomment // line in viewdidload // _tableview.rowheight = uitableviewautomaticdimension; - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { // calculate height based on cell if(!self.customcell) { self.customcell = [self.tableview dequeuereusablecellwithidentifier:@"customcell"]; } // configure cell self.customcell.fontnamelabel.text = _fontarray[indexpath.row]; int quoteindex = indexpath.row % [_quotearray count]; self.customcell.quotelabel.text = _quotearray[quoteindex]; self.customcell.quotelabel2.text = _quotearray[quoteindex]; nsstring *fontname = _fontarray[indexpath.row]; self.customcell.quotelabel.font = [uifont fontwithname:fontname size:17]; self.customcell.quotelabel2.font = [uifont fontwithname:fontname size:17]; // layout cell [self.customcell layoutifneeded]; // height cell cgfloat height = [self.customcell.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize].height; // padding of 1 point (cell separator) cgfloat separatorheight = 1; nslog(@"%f",height); return height + separatorheight; } - (cgfloat)tableview:(uitableview *)tableview estimatedheightforrowatindexpath:(nsindexpath *)indexpath { return 140; } @end
customtableviewcell.h (10 lines)
#import <uikit/uikit.h> @interface customtableviewcell : uitableviewcell @property (weak, nonatomic) iboutlet uilabel *quotelabel; @property (weak, nonatomic) iboutlet uilabel *fontnamelabel; @property (strong, nonatomic) iboutlet uilabel *quotelabel2; @end
customtableviewcell.m (39 lines)
#import "customtableviewcell.h" @implementation customtableviewcell - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // initialization code } return self; } - (void)awakefromnib { // initialization code } - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; // configure view selected state } // code fix layouts setting maxpreferredwidth on uilabel can // multiline – may have similar settings other ui elements // logic fixes layout uilabels don't go margins, // might offset constraint isn't standard. - (void)layoutsubviews { [super layoutsubviews]; [self.contentview layoutifneeded]; self.fontnamelabel.preferredmaxlayoutwidth = self.fontnamelabel.frame.size.width; self.quotelabel.preferredmaxlayoutwidth = self.quotelabel.frame.size.width; self.quotelabel2.preferredmaxlayoutwidth = self.quotelabel2.frame.size.width; } @end
your issue because when use prototype cell created dequeuereusablecellwithidentifier
, cell returned has no size class because not part of view hierarchy , not have traitcollection
associated it.
i worked around temporarily adding cell table view, doing sizing , removing it. while added, customcell acquires size class of controller , layout uses size class related fonts, constraints etc... real cells not have same issue have size class being part of uitableview.
so try changing height calculation follows:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { // calculate height based on cell if(!self.customcell) { self.customcell = [self.tableview dequeuereusablecellwithidentifier:@"customcell"]; } // *** // *** add custom cell table view obtains size class // *** layout. mark needing layout. // *** [self.tableview addsubview:customcell]; [customcell setneedslayout]; // configure cell self.customcell.fontnamelabel.text = _fontarray[indexpath.row]; int quoteindex = indexpath.row % [_quotearray count]; self.customcell.quotelabel.text = _quotearray[quoteindex]; self.customcell.quotelabel2.text = _quotearray[quoteindex]; nsstring *fontname = _fontarray[indexpath.row]; self.customcell.quotelabel.font = [uifont fontwithname:fontname size:17]; self.customcell.quotelabel2.font = [uifont fontwithname:fontname size:17]; // layout cell [self.customcell layoutifneeded]; // height cell cgfloat height = [self.customcell.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize].height; // padding of 1 point (cell separator) cgfloat separatorheight = 1; nslog(@"%f",height); // *** // *** remove cell table view // *** [customcell removefromsuperview]; return height + separatorheight; }
Comments
Post a Comment